Combine multiple cells into one in excel with macro?

后端 未结 3 1974
独厮守ぢ
独厮守ぢ 2021-01-05 07:04

I have a similar question to this one:

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

But I want to combine a range of cells within a

3条回答
  •  一个人的身影
    2021-01-05 07:15

    Based on the thread you are citing, I guess you wish to return the concatination of all the values held by the cells, interpreting all the values as strings?

    For that, you could use a VBA macro that looks like this:

    Function ConcatinateAllCellValuesInRange(sourceRange As Excel.Range) As String
        Dim finalValue As String
    
        Dim cell As Excel.Range
    
        For Each cell In sourceRange.Cells
            finalValue = finalValue + CStr(cell.Value)
        Next cell
    
        ConcatinateAllCellValuesInRange = finalValue
    End Function
    

    As an example, you could call it like this:

    Sub MyMacro()
        MsgBox ConcatinateAllCellValuesInRange([A1:C3])
    End Sub
    

    Is this what you were looking for?

    Mike

提交回复
热议问题