Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop in excel VBA

Tags:

excel

vba

Im trying to use For loop on an excel column. This is my code:

   For Each c In Worksheets("sheet1").Range("A1:A5000").Cells
        c.Offset(0, 1).Range("A1").Value = Right((Left(c, 13)), 7)
   Next

it works ok the problem is with the

Range("A1:A5000")

my sheet has less then 1000 rows but it can grow and I want to be able to use the loop only on the rows that have data in them. how can I change it to go from A1 to the last not empty row?

like image 646
user1040563 Avatar asked May 05 '26 13:05

user1040563


1 Answers

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
like image 158
Hauke P. Avatar answered May 08 '26 13:05

Hauke P.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!