3

我试图在 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 到最后一个非空行?

4

2 回答 2

2
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
于 2013-07-27T10:28:49.950 回答
1

你可以试试这个...

Dim r As Range

Set r = Range("A65536").End(xlup)

For Each c In Worksheets("sheet1").Range("A1:" & r.Address).Cells
   c.Offset(0, 1).Range("A1").Value = Right((Left(c, 13)), 7)
Next
于 2013-07-27T10:30:10.917 回答