|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
返回有数据的最大行列号.zip
(19.2 KB, 下载次数: 21)
老师:B13:C28中,当第三参数大于5时,其计算结果就会出现周而复始的循环现象,为使代码更严谨,似乎应该设置--当第三参数大于5时,1.显示空白;2.指定5时那样,始终显示最后绝对地址。比较而言,我觉得还是显示空白恰当些。
Function EndRC(Optional MyRange As String, 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(1) Else Set sht = ActiveSheet
If MyRange = "" Then Set rng = sht.UsedRange Else Set rng = Intersect(sht.UsedRange, sht.Range(MyRange))
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
|
|