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

后端 未结 4 1096
一整个雨季
一整个雨季 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:05

    In a more general case, here's a macro which concatenates any number of cells (even non-adjacent blocks of cells) Note: I didn't include code which checks user's cancellation.

    Sub G()
    
        Dim strFinal$
        Dim cell As Range
        Dim rngSource As Range
        Dim rngArea As Range
        Dim rngTarget As Range
    
        Set rngSource = Application.InputBox("Select cells to merge", Type:=8)
        Set rngTarget = Application.InputBox("Select destination cell", Type:=8)
        For Each rngArea In rngSource
            For Each cell In rngArea
                strFinal = strFinal & cell.Value & " "
            Next
        Next
        strFinal = Left$(strFinal, Len(strFinal) - 1)
        rngTarget.Value = strFinal
    
    End Sub
    

提交回复
热议问题