使用VBA迭代Excel中的填充行 [英] Iterating through populated rows in Excel using VBA
问题描述
所以我试图通过使用VBA的Excel电子表格中的工作表进行迭代。我想迭代每一行,然后通过每一列,尽管谷歌搜索,我实际上找不到一个直观的方法来做到这一点。
So I am trying to iterate through a worksheet in an Excel spreadsheet using VBA. I want to iterate through each row, and then through each column, and despite googling, I can't actually find an intuitive way to do this.
我假设一行的第一个单元格必须填充,如果不是,则必须是结束。我可以执行这个
I'm assuming that the first cell of a row must be populated, if its not, then that must be the end. I can enforce this
我目前的方法是遍历这些行,然后尝试获取第一个单元格的值,但是我无法理解。我在这里和其他地方遇到使用范围等问题,但没有什么可以帮助我编写代码。
My current approach is to iterate through the rows, then try and get the value of the first cell, but I can't figure it out. I've come questions here and elsewhere around using ranges and such, but nothing that helps me write code.
目前的方法是:
Set sh = ActiveSheet
RowCount = 0
For Each rw In sh.Rows
'If Row.Cells(1, 1).Value = "" Then Exit For
RowCount = RowCount + 1
Next rw
MsgBox (RowCount)
现在,当我运行这个,我得到一些巨大的数字,这是错误的,因为表只有大约25行。我没有评论第一行,因为它不起作用。
Now when I run this, I get some huge number, which is wrong as the table only has about 25 rows. I commented the first line out as it wasn't working.
在For Loop中的第一行我可以改变什么,以便在找到它时正确断开第一个单元格为空的行
推荐答案
看起来你刚刚硬编码行和列;否则,几个小的调整,我想你在那里:
It looks like you just hard-coded the row and column; otherwise, a couple of small tweaks, and I think you're there:
Dim sh As Worksheet
Dim rw As Range
Dim RowCount As Integer
RowCount = 0
Set sh = ActiveSheet
For Each rw In sh.Rows
If sh.Cells(rw.Row, 1).Value = "" Then
Exit For
End If
RowCount = RowCount + 1
Next rw
MsgBox (RowCount)
这篇关于使用VBA迭代Excel中的填充行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!