ExcelVBA最大行Find("*",,,,1,2).Row出错了怎么办

2022-10-31 15:43:31 浏览数 (2)

当工作表中没有数据时

Find("*",,,,1,2).row会出错

所以我自定义了一个函数

代码语言:javascript复制
Sub test()
    With ActiveSheet
'        On Error Resume Next,
        a = .Cells.Find("*", , , , 1, 2).Row
        b = getLastRow(ActiveSheet, 30)
        Debug.Print b
'        On Error GoTo 0
    End With
End Sub
''' 输入工作表,空一维数组arr(1 to x),返回最大行数
Function getLastRow(sht As Worksheet, n As Integer)
    Dim ti As Integer, tarr()
    ReDim tarr(1 To n)
    With sht
        For ti = 1 To n
            tarr(ti) = .Cells(Rows.Count, ti).End(xlUp).Row
        Next ti
    End With
    getLastRow = Application.WorksheetFunction.Max(tarr)
End Function

0 人点赞