如何使用查找功能获取单元格地址。
这是代码
Dim Found As Range
Set Found = Worksheets("Sheet 1").Cells.Find(What:="test", LookAt:=xlWhole, MatchCase:=True)
If Not Found Is Nothing Then
' do something
End If
当我调试代码时,“ Found”变量包含一个“ string”而不是单元格地址。
found.address
即使它显示为字符串,你似乎也可以使用。下面的代码为我工作。
Sub findCellAddress()
Dim ra As Range
Set ra = Cells.Find(What:="fff", LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If ra Is Nothing Then
MsgBox ("Not found")
Else
MsgBox (ra.Address)
End If
End Sub
Find
可以返回Nothing
,因此跟踪.Address
它的结尾只是乞求运行时错误91。 在使用任何属性之前,Find
应始终将其设置为临时Range
对象并进行测试Nothing
。@ nightcrawler23,您好:感谢您的反馈。有什么方法可以分别从“ ra.Address”获取行和列吗?我想将其应用于单元格选择(例如:Cells(1,2))
范围具有行和列属性。因此,您可以使用ra.Row和ra.Column来获取值。msdn.microsoft.com/zh-CN/library/office/ff838238.aspx#Anchor_3
@RandyAdhitama,当您在
.
后面键入点时ra
,应该有一个就地下拉菜单(即IntelliSense),向您显示所有可用的成员。探索一点。当您不确定他们的工作时,可以在线研究他们-一切都已被记录在案。您还可以使用对象浏览器(VBE编辑器中的F2)浏览所有引用的库中的所有类型和成员。