Merge the contents of 2 cells into another 3rd cell using VBA in Excel

后端 未结 4 1093
一整个雨季
一整个雨季 2021-01-20 01:18

I have two cells lets say: A1 and A2

The content of each one of them is a string:

A1: Hallo

A2: World

My goal is to merge the contents of A1

4条回答
  •  时光说笑
    2021-01-20 02:14

    This one is quicker, just select the cells and they are merged into the first cell.

    '------------------------------------------------------------------------
    ' Procedure : Concatenate Text
    ' Author    : Tim Bennett
    ' Date      : 11/6/2015
    ' Purpose   : Concatenate selected text into first column
    '------------------------------------------------------------------------
    '
    'Sub Concatenate_Formula(bConcat As Boolean, bOptions As Boolean)
    Sub Concatenate()
    
    Dim rSelected As Range
    Dim c As Range
    Dim sArgs As String
    Dim bCol As Boolean
    Dim bRow As Boolean
    
        'Set variables
        Set rOutput = ActiveCell
        bCol = False
        bRow = False
    
        On Error Resume Next
    
        'Use current selection
        Set rSelected = Selection
    
        On Error GoTo 0
    
        'Only run if cells were selected and cancel button was not pressed
        If Not rSelected Is Nothing Then
            sArgs = "" 'Create string of cell values
            firstcell = ""
    
            For Each c In rSelected.Cells
                If firstcell = "" Then firstcell = c.Address(bRow, bCol)
                sArgs = sArgs + c.Text + " " 'build string from cell text values
    
                c.Value = "" ' Clear out the cells taken from
            Next
    
            'Put the result in the first cell
            Range(firstcell).Value = sArgs
    
    
    
       End If
    End Sub
    

提交回复
热议问题