最近玩VBA,遇到一个不算大也不算小的问题。就是用EXCEL自己的函数Selection.Find来做检索,最开始是用宏录制发现这个函数的,但在具体应用到自己的代码中的时候,问题出现了:第一,如何指定自己想要查找的字符? 第二,如何取得找到字符的所在位置? 第三,如果未找到字符,如何做返回处理。具体代码如下:
Selection.Find(What:="ABCD", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False)
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False)
前二个疑问很好处理,只是到了第三个问题的时候,不知道怎么处理了。在网上也没有对此相关的帮助文章,最后我想到了一个解决方法,想写出来和大家共享,具体做法如下:
Dim ObjTmp As Object
Set ObjTmp = Selection.Find(What:=FTxt, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False)
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False)
If ObjTmp Is Nothing Then
MsgBox "見つかりません"
else
ObjTmp.Activate
IRow = ActiveCell.row
ICol = ActiveCell.Column
IRow = ActiveCell.row
ICol = ActiveCell.Column
end if