【问题标题】:looping through an unknown range of rows in excel循环遍历excel中未知的行范围
【发布时间】:2015-04-23 03:36:31
【问题描述】:

说我有 5 列,天知道有多少行。除了最后一列中的几个单元格外,此范围内的所有单元格都已填充。 我需要遍历所有行(从 A1 开始)直到我到达最后一行,并且对于在最后一列中填充单元格的每一行,显示一个消息框说“你好”

我真的不确定如何开始循环。 我试过谷歌搜索,但我不明白。我知道如何检查空单元格,以及如何查看消息框,但不知道如何找到范围的结尾。

【问题讨论】:

标签: vba loops excel


【解决方案1】:

我这边的其他变种

Dim cl As Range
With ActiveSheet
    For Each cl In .Range(.[A1].CurrentRegion.Columns(5).Address)
        If cl.Value <> "" Then
            MsgBox cl.Address(0, 0) & " has a value (" & cl.Value & ")"
        End If
    Next cl
End With

【讨论】:

    【解决方案2】:

    只要没有完全空白的列或行,.currentregion 属性就应该为您提供对完整矩形单元格块的引用。

    dim rw as long
    with activesheet.cells(1, 1).currentregion
        for rw = 1 to .rows.count
            if not isempty(.cells(rw, 5)) then
                msgbox .cells(rw, 5).address(0, 0) & " has a value (" & .cells(rw, 5).value & ")"
            end if
        next rw
    end with
    

    【讨论】:

      【解决方案3】:

      很多版本

      Sub Button1_Click()
          Dim Rws As Long, Rng As Range, c As Range, x
          Rws = Cells(Rows.Count, "A").End(xlUp).Row
          Set Rng = Range(Cells(1, 1), Cells(Rws, 1))
      
          For Each c In Rng.Cells
              x = WorksheetFunction.CountA(Range(Cells(c.Row, 1), Cells(c.Row, 5)))
              If x = 5 Then MsgBox "Hello! Row #- " & c.Row & " has 5!"
          Next c
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2010-11-30
        • 1970-01-01
        • 2013-08-17
        • 1970-01-01
        • 2016-01-01
        • 2020-12-10
        • 1970-01-01
        • 1970-01-01
        • 2016-12-21
        相关资源
        最近更新 更多