如果工作表中存在数据,则返回最后一行

时间:2018-04-05 21:15:34

标签: excel vba excel-vba

我正在编写一个函数,用于检查现有工作表是新的还是有数据。如果它包含数据,那么它应该返回最后一行,否则它必须返回第一行。我使用以下代码:

Private Function GetLastRow(sheetName As String) As Integer
Dim lastRow As Integer
lastRow = CurrentWorkbook.Sheets(sheetName).Cells.Find(What:="*", _
                After:=Range("A1"), _
                LookAt:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).End(xlUp).Row

GetLastRow = lastRow
End Function

但是在调试时,我收到的错误是没有设置对象。 我的代码中有错误吗?

4 个答案:

答案 0 :(得分:1)

喜欢这个

Option Explicit

Public Sub TEST()

Debug.Print GetLastRow(ActiveSheet.Name)

End Sub

Private Function GetLastRow(ByVal sheetName As String) As Long

    Dim lastRow As Long

    With ActiveWorkbook.Sheets(sheetName)

        On Error GoTo returnVal

        lastRow = .Cells.Find(What:="*", _
                              After:=.Range("A1"), _
                              LookAt:=xlPart, _
                              LookIn:=xlFormulas, _
                              SearchOrder:=xlByRows, _
                              SearchDirection:=xlPrevious, _
                              MatchCase:=False).Row
    End With

    GetLastRow = lastRow

    Exit Function

returnVal:

    GetLastRow = 1

End Function

答案 1 :(得分:1)

使用这些功能,您始终可以找到最后一行和最后一列,您可以提供要查找最后一行或最后一列的工作表,更重要的是,您可以使用哪个列或行作为标准。如果省略参数,它将使用活动工作表作为工作表,它将使用第一列查找最后一行/单元格和第一行以查找最后一列/单元格

Function LastRowInColumn(Optional sh As Worksheet, Optional colNumber As Long = 1) As Long
    'Finds the last row in a particular column which has a value in it
    If sh Is Nothing Then
        Set sh = ActiveSheet
    End If
    LastRowInColumn = sh.Cells(sh.Rows.Count, colNumber).End(xlUp).row
End Function

Function LastColumnInRow(Optional sh As Worksheet, Optional rowNumber As Long = 1) As Long
    'Finds the last column in a particular row which has a value in it
    If sh Is Nothing Then
            Set sh = ActiveSheet
        End If
    LastColumnInRow = sh.Cells(rowNumber, sh.Columns.Count).End(xlToLeft).Column
End Function

答案 2 :(得分:1)

这是我使用的那个:

Public Function GetLastRow(ByVal arg_ws As Worksheet) As Long

    Dim rTest As Range

    Set rTest = arg_ws.Cells.Find("*", arg_ws.Range("A1"), xlFormulas, xlPart, , xlPrevious)
    If Not rTest Is Nothing Then GetLastRow = rTest.Row Else GetLastRow = 0

End Function

这样称呼:

Sub tst()

    MsgBox GetLastRow(ActiveWorkbook.Sheets("Sheet1"))

End Sub

答案 3 :(得分:0)

完全引用父工作表并使用long,而不是整数。结束(xlup)是多余的。

Private Function GetLastRow(sheetName As String) As Long
    Dim lastRow As long

    with ActiveWorkbook.workSheets(sheetName)
        lastRow = .Cells.Find(What:="*", _
                              After:=.Range("A1"), _
                              LookAt:=xlPart, _
                              LookIn:=xlFormulas, _
                              SearchOrder:=xlByRows, _
                              SearchDirection:=xlPrevious, _
                              MatchCase:=False).Row
    end with
    GetLastRow = lastRow
End Function