首先要知道行号、列号是已知还是未知的,知道行号、列号确定单元格以后,用CELLS就可以了,把下面的代码复制到VBA里面执行就看出来了:
Sub tt()
Dim a, b As Integer
a = InputBox("请输入行号")
b = InputBox("请输入列号")
Cells(a, b).Select
End Sub
这上面就是单元格选定。
Sub a()
Dim i As Integer
For i = 0 To UBound(Split(Selection.Address, ","))
MsgBox Split(Split(Selection.Address, ",")(i), "$")(2)
Next
End Sub
刚才又思考了一下,下面代码更加简单实用强大。
Sub test()
For Each c In Selection.Rows
MsgBox c.Row
Next
End Sub
选中一行,选中连接多行,选中不连接多行都适用,请测试:
(刚才的有问题,又改了一下)
Sub b()
If Selection.Address Like "*,*" Then
p = Split(Selection.Address, ",")
For i = LBound(p) To UBound(p)
q = Split(p(i), "$")
MsgBox q(UBound(q))
Next
Else
p = Split(Selection.Address, "$")
For i = Replace(p(1), ":", "") To p(2)
MsgBox i
Next
End If
End Sub
这是选中整行的,如果你选中的是区域就有问题,有问题的话Q我的EXCEL群找吧,这里不让再改了——84253041