|
楼主 |
发表于 2019-2-6 21:44
|
显示全部楼层
本帖最后由 小花鹿 于 2019-2-21 07:39 编辑
=============================================================
修改一下,可以求行号和列号,有兴趣的帮忙测试一下:
Sub test2()
Dim s1$, s2$, s
s1 = [m1]
s2 = [m2]
s = EndRC(s1, s2)
MsgBox s(1) & "," & s(2)
End Sub
Function EndRC(Optional MyRange As String, Optional MySht As String)
'函数返回结果为两个元素的一维数组,EndRC(1)代表最后行号,EndRC(2)代表最后列号
'得到指定范围的有数据的最后行列号,两个参数均可选
'参数形式如:MyRange是指定范围,形如,整列"A:B"、整行"6:26"、矩形区域"a6:h26",如果忽略则为UsedRange
' MySht是指工作表,如"sheet1",如果忽略则为活动工作表
Dim rng As Range, sht As Worksheet, ar, i&, j&, RC(1 To 2) As Long
If MySht <> "" Then
Set sht = Sheets(MySht)
Else
Set sht = ActiveSheet
End If
If MyRange = "" Then
Set rng = sht.UsedRange
Else
Set rng = Intersect(sht.UsedRange, sht.Range(MyRange))
End If
If rng Is Nothing Then
GoTo label1
End If
ar = rng.Value
If Not IsArray(ar) Then
If Len(ar) = 0 Then
GoTo label1
Else
RC(1) = rng.Row
RC(2) = rng.Column
GoTo label1
End If
Else
For i = UBound(ar, 2) To 1 Step -1
For j = 1 To UBound(ar)
If Len(ar(j, i)) Then
RC(2) = i - 1 + rng.Column
GoTo label2
End If
Next j
Next i
label2:
For i = UBound(ar) To 1 Step -1
For j = 1 To UBound(ar, 2)
If Len(ar(i, j)) Then
RC(1) = i - 1 + rng.Row
GoTo label1
End If
Next j
Next i
End If
label1:
EndRC = RC
End Function
|
|