小编Cas*_*ieD的帖子

非连续范围中的最后一行或单元格{NOT a Worksheet search}

首先,这不是典型的新手问题.我正在尝试找到我已经正确创建的范围中的最后一行(或单元格).

在这个例子中,我有两个范围合并为一个与union.在整个范围内循环,我得到了我想要的数据,并且工作得很好.

Sub Test()

    Dim unionRange As Range, currentRow As Range
    Set unionRange = Union(Range("A3:C5"), Range("A8:C11"))

    For Each currentRow In unionRange.Rows
        Debug.Print "Row: " & currentRow.row
    Next currentRow

End Sub
Run Code Online (Sandbox Code Playgroud)

这给出了这些结果

Row: 3
Row: 4
Row: 5
Row: 8
Row: 9
Row: 10
Row: 11
Run Code Online (Sandbox Code Playgroud)

但是当我想确定当前行是否是我的范围中的最后一行时,我似乎无法找到获取该信息的干净方法.

Sub Test()

    Dim unionRange As Range, currentRow As Range
    Set unionRange = Union(Range("A3:C5"), Range("A8:C11"))

    For Each currentRow In unionRange.Rows
        Dim lastRowIndex As Integer

        'lastRowIndex = unionRange. ???

        If currentRow.row = lastRowIndex Then
            MsgBox ("Last …
Run Code Online (Sandbox Code Playgroud)

excel vba excel-vba

4
推荐指数
1
解决办法
872
查看次数

标签 统计

excel ×1

excel-vba ×1

vba ×1