我正在尝试在 Excel 列上使用 For 循环。这是我的代码:
For Each c In Worksheets("sheet1").Range("A1:A5000").Cells
c.Offset(0, 1).Range("A1").Value = Right((Left(c, 13)), 7)
Next
工作正常,问题出在
Range("A1:A5000")
我的工作表少于 1000 行,但它可以增长,并且我希望能够仅在包含数据的行上使用循环。 如何将其更改为从 A1 到最后一个非空行?
最佳答案
Dim RowIndex As Long
RowIndex = 1
Dim c
While Not IsEmpty(Worksheets("sheet1").Cells(RowIndex, 1))
Set c = Worksheets("sheet1").Cells(RowIndex, 1)
c.Offset(0, 1).Range("A1").Value = Right((Left(c, 13)), 7)
RowIndex = RowIndex + 1
Wend
关于vba - Excel VBA 中的 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17896655/