简体   繁体   English

Excel VBA:查找最后一行

[英]Excel VBA: Finding last row

I am trying to find the last row of a set of numbers for each column, however it seems my code is using the same last row as the previous column's last row.我试图为每一列找到一组数字的最后一行,但似乎我的代码使用的最后一行与前一列的最后一行相同。

Here is the first column's code:这是第一列的代码:

Dim WorkRng As Range
xTitleId = "Select Total Sales cell"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
WorkRng.Select
Selection.Copy
Range("A65") = "Total Sales"
Range("A66").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Set WorkRng = Range("A66")

Here is the adjacent column's code:这是相邻列的代码:

Dim WorkRng2 As Range
xTitleId2 = "Select Collected Range"
Set WorkRng2 = Application.Selection
Set WorkRng2 = Application.InputBox("Range", xTitleId2, WorkRng2.Address, Type:=8)
WorkRng2.Select
Selection.Copy
Range("B65") = "Collected Range"
Range("B66").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Range("B66:B100").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
Range("B66").Select
Dim LastRow2 As Long
LastRow2 = Cells(Rows.Count, 1).End(xlUp).Row
Range("B" & LastRow2).Offset(1, 0).Formula = "=SUM(B66:B" & LastRow2 & ")"
Range("B" & LastRow2).Offset(1, 0).Select
Set WorkRng2 = Range("B" & LastRow2).Offset(1, 0)

It keeps saying that LastRow2 is 66, however I would like it to be dynamic and independent of the first column's last row.它一直说 LastRow2 是 66,但是我希望它是动态的并且独立于第一列的最后一行。

Any help is greatly appreciated任何帮助是极大的赞赏

You can add a VBA Function like the below: 您可以添加VBA功能,如下所示:

    Function GetLastRow(wsTarget As Worksheet, iCol)
    'Gets the last non-blank cell
    Dim lrow As Long, Max_Row

    Max_Row = 1048576
        On Error GoTo SmallFile

    TryAgain:
        With wsTarget
            lrow = .Cells(Max_Row, iCol).End(xlUp).Row
        End With

        GetLastRow = lrow

        Exit Function
    SmallFile:
        Max_Row = 65536
        On Error GoTo 0
        GoTo TryAgain

    End Function

Then you specificy the worksheet and column number you're after. 然后,指定要使用的工作表和列号。 ie GetLastRow(worksheets("Sheet1"),1) would return the last row for Column A in the Sheet1 tab. 即GetLastRow(worksheets(“ Sheet1”),1)将返回Sheet1选项卡中列A的最后一行。

Beware of the fact, that xlUp gets last visible row, which can cause problems, if filters are present.请注意,xlUp 获取最后一个可见行,如果存在过滤器,这可能会导致问题。 Therefore, if filters might be active, use rather:因此,如果过滤器可能处于活动状态,请使用:

Function getLastRow(col As String, ws As Worksheet) As Long
    Call removeFilters(ws)
    getLastRow = ws.Range(col & Rows.Count).End(xlUp).Row

End Function

Sub removeFilters(ws As Worksheet)
    On Error Resume Next
    ws.ShowAllData

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM