Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 动态逐行连接列值_Vba_Excel - Fatal编程技术网

Vba 动态逐行连接列值

Vba 动态逐行连接列值,vba,excel,Vba,Excel,我的代码连接表“Data”第一行的列的值,并将结果写入表“Insert”单元格A1 错误是下一行的连接结果被添加到表“Insert”A1中第一个结果的末尾,以此类推。结果也应逐行写入“插入”表中的列中 我的代码有什么问题 Sub InsertStatementRow() Dim x As String Dim rng As Range Dim cel As Range Dim ColMax As Integer Dim i As Long Sheets("Data").Select

我的代码连接表“Data”第一行的列的值,并将结果写入表“Insert”单元格A1

错误是下一行的连接结果被添加到表“Insert”A1中第一个结果的末尾,以此类推。结果也应逐行写入“插入”表中的列中

我的代码有什么问题

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

    Sheets("Data").Select

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

    row = 1
    Do While Cells(row, "A").Value <> ""

        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

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

    row = row + 1
    Loop

End Sub
子InsertStatementRow()
将x作为字符串
变暗rng As范围
暗淡的cel As范围
Dim ColMax作为整数
我想我会坚持多久
工作表(“数据”)。选择
ColMax=单元格(1,Columns.Count).End(xlToLeft).Column
行=1
Do While单元格(行,“A”)。值“”
带工作表(“数据”)
i=1
设置rng=范围(.Cells(i,1),.Cells(i,ColMax))
以
对于rng中的每个cel
x=x&单元值
下一个
表格(“插入”)。单元格(i,1)。值=x
行=行+1
环
端接头
谢谢你的帮助

您设置了

i=1
以前

Set rng = Range(.Cells(i, 1), .Cells(i, ColMax))
但您似乎正在使用变量
。因此,删除/注释行
i=1
并更改
Set rng

Set rng = Range(.Cells(row, 1), .Cells(row, ColMax))
希望它能解决您的问题。

每次Do While循环开始时,我都被设置为“1”,因此rng变量将始终引用数据工作表的第1行。它还复制到插入工作表的第1行。将i更改为行,它应该可以工作:

Do While Cells(row, "A").Value <> ""
    With Worksheets("Data")
        Set rng = Range(.Cells(row, 1), .Cells(row, ColMax))
    End With

    x = ""

    For Each cel In rng

        x = x & cel.Value

    Next

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

row = row + 1
Loop
Do While单元格(行,“A”)。值“”
带工作表(“数据”)
设置rng=范围(.Cells(行,1),.Cells(行,ColMax))
以
x=“”
对于rng中的每个cel
x=x&单元值
下一个
表格(“插入”)。单元格(第1行)。值=x
行=行+1
环