遍历填充行 [英] Iterating through populated rows

查看:35
本文介绍了遍历填充行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试使用 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 across some questions here and elsewhere that use 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 循环的第一行找到第一个单元格为空的行时,我可以更改什么以正确中断?

推荐答案

看起来你只是硬编码了行和列;否则,进行一些小调整,我想你就在那里:

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)

这篇关于遍历填充行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆