问题
I want to find the last row with text in column A of an excel spreadsheet using a while loop. I'm not sure as to the exact syntax that I need, since I'm new to VBA. I realize there are other ways to find the last row, however I need to use a while loop starting at row 20.
Dim Row As Integer
Row = 20
Do While .Range("A" & Row) <> ""
Row = Row + 1
Loop
回答1:
Your code will only find the first empty cell in that column, which is not necessarily after the last used row. Why must it be a "While" loop? There are half a dozen ways to find the last used row or column without the while loop. Simply google "vba excel find last row"
回答2:
Loop to the last row with this:
Dim X as long 'Note, use Long, NOT Integer
For X = 20 to Range("A" & rows.count).end(xlup).row
'What you want to do in your loop here
Next
Do while is not a good option here as others have pointed out, it will stop when it gets to a blank even if there is data on the cells afterwards.
Personally I try to stay away from naming variables the same as an object or method in VBA.
来源:https://stackoverflow.com/questions/42081800/finding-last-row-with-while-loop-in-excel