Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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
Excel 将一行拆分为多行的VBA将返回额外数据_Excel_Vba - Fatal编程技术网

Excel 将一行拆分为多行的VBA将返回额外数据

Excel 将一行拆分为多行的VBA将返回额外数据,excel,vba,Excel,Vba,我是VBA新手,需要一些代码方面的帮助 我有一个大的电子表格,有几千行,每行有一个唯一的ID,问题的答案从7到65个。我需要按id拆分这些行,以便每行都有id、问题编号和数值(问题答案) 原始数据如下所示: 我发现了一个VBA代码,该代码帮助我在此页面上拆分行: 这是我使用的代码: Sub NewLayout() For i = 2 To Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row For j = 0 To Cells.Fi

我是VBA新手,需要一些代码方面的帮助

我有一个大的电子表格,有几千行,每行有一个唯一的ID,问题的答案从7到65个。我需要按id拆分这些行,以便每行都有id、问题编号和数值(问题答案)

原始数据如下所示:

我发现了一个VBA代码,该代码帮助我在此页面上拆分行:

这是我使用的代码:

Sub NewLayout()
For i = 2 To Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
    For j = 0 To Cells.Find("*", [A1], , , xlByColumns, xlPrevious).Column
        If Cells(i, 13 + j) <> vbNullString Then
            intCount = intCount + 1
            Cells(i, 1).Copy Destination:=Cells(intCount, 30)
            Cells(i, 2 + j).Copy Destination:=Cells(intCount, 31)
            Cells(i, 13 + j).Copy Destination:=Cells(intCount, 32)
        End If
    Next j
Next i
End Sub
Sub-NewLayout()
对于i=2的单元格。查找(“*”,[A1],.xlByRows,xlPrevious)。行
对于j=0的单元格。查找(“*”,[A1],.xlByColumns,xlPrevious)。列
如果单元格(i,13+j)为空字符串,则
intCount=intCount+1
单元格(i,1)。复制目标:=单元格(intCount,30)
单元格(i,2+j)。复制目标:=单元格(intCount,31)
单元格(i,13+j)。复制目标:=单元格(intCount,32)
如果结束
下一个j
接下来我
端接头
代码似乎可以工作,但我对输出有问题:它返回一些额外的数据,我不知道代码中的错误在哪里以及如何消除它

输出示例附在后面

非常感谢您的帮助

试试看

Sub NewLayout()
    with worksheets("sheet1")
        For i = 2 To .Cells(.rows.count, "A").end(xlup).Row
            For j = 0 To .Cells(i, 29).end(xltoleft).Column
                .cells(.rows.count, "AD").end(xlup).offset(1,0) = .cells(i, "A").value
                .cells(.rows.count, "AD").end(xlup).offset(0,1) = .cells(1, j).value
                .cells(.rows.count, "AD").end(xlup).offset(1,0) = .cells(i, j).value
            Next j
        Next i
    end with
End Sub
您的终止
j
正在嵌套For中重新评估。。。下一步,当你将值写入列广告:AF时,你可能会考虑写另一个工作表或低于你现有的数据而不是右边。

< P>试着作为,

Sub NewLayout()
    with worksheets("sheet1")
        For i = 2 To .Cells(.rows.count, "A").end(xlup).Row
            For j = 0 To .Cells(i, 29).end(xltoleft).Column
                .cells(.rows.count, "AD").end(xlup).offset(1,0) = .cells(i, "A").value
                .cells(.rows.count, "AD").end(xlup).offset(0,1) = .cells(1, j).value
                .cells(.rows.count, "AD").end(xlup).offset(1,0) = .cells(i, j).value
            Next j
        Next i
    end with
End Sub
您的终止
j
正在嵌套For中重新评估。。。下一步,当你将值写入列广告:AF时,你可能会考虑写另一个工作表或低于你现有的数据而不是右边。