Find last row in range

前端 未结 11 2007
傲寒
傲寒 2020-12-02 00:41

I\'m having a little trouble with finding the last row.

What I am trying to do is find the last row in column "A", then use that to find the last row within

相关标签:
11条回答
  • 2020-12-02 01:27
    Dim rng As Range
    Dim FirstRow, LastRow As long
    
    Set rng = Selection
    
    With rng
    
     FirstRow = ActiveCell.Row
    
     LastRow = .Rows(.Rows.Count).Row
    
    End With
    
    
    0 讨论(0)
  • 2020-12-02 01:30
        'This is sure method to find or catch last row in any column even   'if  some cell are blank in-between. (Excel-2007)` 
    'This works even if sheet is not active
    
        'mycol is the column you want to get last row number
    
    for n=1048575 to 1 step -1
    myval=cells(n,mycol)
    if myval<>"" then
    mylastrow=n 'this is last row in the column
    exit for
    end if
    next
    
    ret=msgbox("Last row in column-" & mycol & "is=" & mylastrow)
    
    0 讨论(0)
  • 2020-12-02 01:32

    I came here looking for a way to find the last row in a non-contiguous range. Most responses here only check one column at a time so I created a few different functions to solve this problem. I will admit, though, that my .Find() implementation is essentially the same as Shai Rado's answer.

    Implementation 1 - Uses Range().Find() in reverse order

    Function LastRowInRange_Find(ByVal rng As Range) As Long
    
        'searches range from bottom up stopping when it finds anything (*)
        Dim rngFind As Range
        Set rngFind = rng.Find( What:="*", _
                                After:=Cells(rng.row, rng.Column), _
                                LookAt:=xlWhole, _
                                LookIn:=xlValues, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlPrevious)
    
        If Not rngFind Is Nothing Then
            LastRowInRange_Find = rngFind.row
        Else
            LastRowInRange_Find = rng.row
        End If
    
    End Function
    

    Implementation 2 - Uses Range().End(xlUp) on each column

    Function LastRowInRange_xlUp(ByVal rng As Range) As Long
    
        Dim lastRowCurrent As Long
        Dim lastRowBest As Long
    
        'loop through columns in range
        Dim i As Long
        For i = rng.Column To rng.Column + rng.Columns.count - 1
            If rng.Rows.count < Rows.count Then
                lastRowCurrent = Cells(rng.row + rng.Rows.count, i).End(xlUp).row
            Else
                lastRowCurrent = Cells(rng.Rows.count, i).End(xlUp).row
            End If
    
            If lastRowCurrent > lastRowBest Then
                lastRowBest = lastRowCurrent
            End If
        Next i
    
        If lastRowBest < rng.row Then
            LastRowInRange_xlUp = rng.row
        Else
            LastRowInRange_xlUp = lastRowBest
        End If
    
    End Function
    

    Implementation 3 - Loops through an Array in reverse order

    Function LastRowInRange_Array(ByVal rng As Range) As Long
    
        'store range's data as an array
        Dim rngValues As Variant
        rngValues = rng.Value2
    
        Dim lastRow As Long
    
        Dim i As Long
        Dim j As Long
    
        'loop through range from left to right and from bottom upwards
        For i = LBound(rngValues, 2) To UBound(rngValues, 2)                'columns
            For j = UBound(rngValues, 1) To LBound(rngValues, 1) Step -1    'rows
    
                'if cell is not empty
                If Len(Trim(rngValues(j, i))) > 0 Then
                    If j > lastRow Then lastRow = j
    
                    Exit For
                End If
    
            Next j
        Next i
    
        If lastRow = 0 Then
            LastRowInRange_Array = rng.row
        Else
            LastRowInRange_Array = lastRow + rng.row - 1
        End If
    
    End Function
    

    I have not tested which of these implementations works fastest on large sets of data, but I would imagine that the winner would be _Array since it is not looping through each cell on the sheet individually but instead loops through the data stored in memory. However, I have included all 3 for variety :)


    How to use

    To use these functions, you drop them into your code sheet/module, specify a range as their parameter, and then they will return the "lowest" filled row within that range.

    Here's how you can use any of them to solve the initial problem that was asked:

    Sub answer()
    
        Dim testRange As Range
        Set testRange = Range("A1:F28")
    
        MsgBox LastRowInRange_Find(testRange)
        MsgBox LastRowInRange_xlUp(testRange)
        MsgBox LastRowInRange_Array(testRange)
    
    End Sub
    

    Each of these will return 18.

    0 讨论(0)
  • 2020-12-02 01:35

    If your wbshtSelect is defined as worksheet and you have used set to define the specific worksheet, you can use this.

     Dim LastRow As Long
    
     wbshtSelect.UsedRange ' Refresh UsedRange
     LastRow = wbshtSelect.UsedRange.Rows(wbshtSelect.UsedRange.Rows.Count).Row
    

    Otherwise take a look here http://www.ozgrid.com/VBA/ExcelRanges.htm

    0 讨论(0)
  • 2020-12-02 01:37

    Range().End will bring you to the end of a code block. If the starting cell is empty, it brings you the the first used cell or the last cell. It the cells is not empty it brings you to the last used cell. For this reason, you need to test whether or not the cell in column B is to determine whether to use LR_wbSelectNew as the last row.

    With wbshtSelect
        LR_wbSelect = .Cells(Rows.Count, "A").End(xlUp).Row - 22
    
        If .Cells(LR_wbSelect, "B") <> "" Then
            LR_wbSelectNew = LR_wbSelect
        Else
            LR_wbSelectNew = .Cells(LR_wbSelect, "B").End(xlUp).Row
        End If
    End With
    

    This code defines a Target range that extends from A1 to the last row in column a - 22 and extends 10 columns.

    Dim Target As Range
    With wbshtSelect
        Set Target = .Range("A1", .Cells(Rows.Count, "A").End(xlUp).Offset(-22)).Resize(, 10)
    End With
    
    0 讨论(0)
提交回复
热议问题