Finding last row with while loop in excel

☆樱花仙子☆ 提交于 2019-12-14 04:13:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!