更多有关Cells.find 函数的信息

Sub Test()

Dim yzfRange As Range

Set yzfRange = Cells.Find(What:="Yuanzifan.com", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart,  SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)文章源自原紫番博客-https://www.yuanzifan.com/4536.html

 文章源自原紫番博客-https://www.yuanzifan.com/4536.html

Cells.Find(What:="Yuanzifan.com", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart,      SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True).Activate文章源自原紫番博客-https://www.yuanzifan.com/4536.html

End Sub文章源自原紫番博客-https://www.yuanzifan.com/4536.html

在Set方法中,Cells.Find的返回值为一个对象。第一段代码是在文件中查找字符串“Yuanzifan.com”,如果查到,则yzfRange的值为一个对象,此对象包含了目标字符串所在单元格的行数、列数、字符串本身的值以及目标单元格的其他属性。但如果使用msgbox将其弹出,则被弹出的值只是目标字符串的内容而已。文章源自原紫番博客-https://www.yuanzifan.com/4536.html

比如,如果在第一段代码之后加上 Msgbox yzfRange  ,则弹出窗体的内容为Yuanzifan.com文章源自原紫番博客-https://www.yuanzifan.com/4536.html

如果没有找到目标值,则返回值为Nothing.(注意,是Nothing,不是Null,也不是空,更不是其它)文章源自原紫番博客-https://www.yuanzifan.com/4536.html

如果不用Set,则Cells.Find可用自己本身的一些方法,这些方法可以通过在“.”之后调用。比如最常见的Activate方法。如果目标字符串没有找到,则系统将报错退出。所以用到这个的时候,最好做好异常处理。文章源自原紫番博客-https://www.yuanzifan.com/4536.html

另外,上述语句只能写在Module之中,如果写在活动单元格之中,则会报代码为91的错误。文章源自原紫番博客-https://www.yuanzifan.com/4536.html 文章源自原紫番博客-https://www.yuanzifan.com/4536.html

 

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证