动态连接单元格值并将结果复制到另一个工作表

时间:2014-07-03 15:11:29

标签: vba excel-vba excel

我有一个带有x列和y行的工作表(“数据”)。我想连接一行的值,并且连接的结果应该被复制到同一行的第一列中的第二个工作表(“Insert”)。

我试过这个VBA并收到错误消息

Sub InsertStatementRow()
Dim x As String, rng As Range, rng1 As Range, cel As Range
Dim ColMax As Integer
Dim i As Long

    Sheets("Data").Select
    Range("A1").Select

        ColMax = Cells(1, Columns.Count).End(xlToLeft).Column

        With Worksheets("Data")
        i = 1
        Set rng = Range(Cells(i, 1), Cells(i, ColMax))
        End With

        For Each cel In rng

            x = x & cel.Value

        Next

        Range(Sheets("Insert").Cells(i, 1)).Value = x

End Sub

请通过更正我的代码告诉我我做错了什么。谢谢!

1 个答案:

答案 0 :(得分:1)

使用一些"。"

Set rng = Range(.Cells(i, 1), .Cells(i, ColMax))