VBA怎么把每一列数据合并成一个单元格内容

img


VBA怎么把每一列数据合并成一个单元格内容,如图所示。麻烦在代码后面注明一下,方便我修改。谢谢了

这个问题涉及以下知识点
1.vba的for循环
2.vba怎么获取sheet数据区域的最大行号
3.vba中字符串拼接
4.vba数组

Sub Demo()
    Dim i As Integer, j As Integer, msg As String
    Dim msgArray(5) As String
    ' 列数
    colNum = 5
    
     ' 最后一行的行号
    lastLineNum = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 1 To lastLineNum
        For j = 1 To colNum
            msgArray(j) = msgArray(j) & Cells(i, j)
        Next
    Next
    
    
    For j = 1 To colNum
        Cells(j, colNum + 1) = msgArray(j)
    Next
End Sub

谢谢您哈