如何使用 VBA 连续复制不同长度的单元格

How to copy cells in a row with varying lengths using VBA

我正在尝试复制一行中的单元格,但每一行的长度都不一样。 例如:Row1 的数据来自 A1:H1 Row2 的数据来自 A2:L2 第 3 行有来自 A3:D3 的数据 通常的代码是 .Range(.Cells(i,"A"),.Cells(i,"H")).Copy 但是在一个循环中,我该如何检测行的长度?

谢谢!

试试这个:

Sub CopyRows
    Dim intCounter as Integer
    For intCounter = 1 to Cells(Rows.Count,1).End(xlUp).Row
        Range(Cells(intcounter,1), Cells(intcounter,Columns.Count).End(xlToLeft).Column)).Copy
        ' Copy your stuff, where you want
    Next intCounter
End sub

在您的具体情况下:

.Range(.Cells(i,1),.Cells(i,.Cells(i,Columns.Count).End(xlToLeft).Column)).Copy