|
老师,像下面那样,倒数第二句? 不行啊,仍然显示#VALUE!错误。
Function ENDRC(Optional MyRange As Range, Optional MySht As String, Optional mode = 0)
'得到有数据的最后行号,两个参数均可选
'参数形式如:MyRange 指定范围,如果忽略则为UsedRange
' A:B 整列
' 6:26 整行
' a6:h26 矩形区域
' "" UsedRange
' MySht 指工作表名,如果忽略则为活动工作表
' 在工作表中使用且MySht=活动工作表时,会引起循环引用,需设置手动计算Application.Calculation = xlManual或启用迭代计算Application.Iteration = True
' mode 返回模式,0~9最大行(行扫描优先),>9最大列(列扫描优先)
' 0 最后相对地址
' 1 最后行号
' 2 最后列号
' 3 最后列字母
' 4 "最后行号,最后列号"
' 5 最后绝对地址
Dim rng As Range, sht As Worksheet, ar, i As Long, j As Long
If MySht <> "" Then Set sht = Sheets(2) Else Set sht = ActiveSheet
If MyRange Is Nothing Then Set rng = sht.UsedRange Else Set rng = Intersect(sht.UsedRange, sht.Range(MyRange.Address))
If Not rng Is Nothing Then
ar = rng.Value
If Not IsArray(ar) Then
If Len(ar) > 0 Then EndR = rng.Row: EndC = rng.Column
ElseIf mode < 10 Then
For i = UBound(ar) To 1 Step -1
For j = UBound(ar, 2) To 1 Step -1
If Len(ar(i, j)) Then
EndR = i - 1 + rng.Row
EndC = j - 1 + rng.Column
i = 1
Exit For
End If
Next
Next
Else
For j = UBound(ar, 2) To 1 Step -1
For i = UBound(ar) To 1 Step -1
If Len(ar(i, j)) Then
EndR = i - 1 + rng.Row
EndC = j - 1 + rng.Column
j = 1
Exit For
End If
Next
Next
mode = mode Mod 10
End If
If mode = 1 Then
ENDRC = EndR
ElseIf mode = 2 Then
ENDRC = EndC
ElseIf mode = 3 Then
ENDRC = Split(Cells(EndR, EndC).Address, "$")(1)
ElseIf mode = 4 Then
ENDRC = EndR & "," & EndC
ElseIf mode = 5 Then
ENDRC = Cells(EndR, EndC).Address
Else
ENDRC = Cells(EndR, EndC).Address(0, 0)
End If
End If
If IsError(ENDRC) Then ENDRC = ""
End Function
|
|