问题描述
我想使用 while 循环在 Excel 电子表格的 A 列中找到带有文本的最后一行.我不确定我需要的确切语法,因为我是 VBA 新手.我意识到还有其他方法可以找到最后一行,但是我需要使用从第 20 行开始的 while 循环.
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
推荐答案
循环到最后一行:
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 在这里不是一个好的选择,即使之后单元格上有数据,它也会在变为空白时停止.
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.
就我个人而言,我尽量避免将变量命名为 VBA 中的对象或方法.
Personally I try to stay away from naming variables the same as an object or method in VBA.
这篇关于在excel中使用while循环查找最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!