Excel VBA 使用 .Find 和 .FindNext 查找精确字符串

Posted

技术标签:

【中文标题】Excel VBA 使用 .Find 和 .FindNext 查找精确字符串【英文标题】:Excel VBA finding an exact string using .Find and .FindNext 【发布时间】:2015-08-13 21:28:00 【问题描述】:

我在使用 .Find 和 .FindNext 时遇到问题。我想准确搜索字符串“Q1”。目前,当我的 .FindNext 函数运行时,它会在一个以“Q10”为值的单元格中找到“Q1”。如何限制 .Find 和/或 .FindNext 函数来搜索我想要的确切字符串? 感谢您的帮助!

这是我的代码。例如,我想在可能具有 Q1、Q3、Q5、Q10、Q11、Q1、Q2、Q1 等的单元格范围内查找“Q1”的第三个实例。

Dim findValue As Range

' find first instance of Q1
searchString = "Q1" 
Set findValue = ActiveWorkbook.Sheets("Sheet1").Cells.Find(What:=searchString)
findValue.select

' set loopMax to 2 because we want to .Findnext to run two more times to arrive at the third instance of "Q1" 
For counter = 1 To loopMax
Set findValue = ActiveWorkbook.Sheets("Sheet1").Cells.FindNext(findValue)
findValue.select
Next counter  

我被困住了。任何帮助将不胜感激!

【问题讨论】:

需要在您对Find()的调用中添加lookat:=xlWhole 老兄,谢谢!!你们是救生员。 【参考方案1】:

需要在调用 Find() 时添加 lookat:=xlWhole

【讨论】:

以上是关于Excel VBA 使用 .Find 和 .FindNext 查找精确字符串的主要内容,如果未能解决你的问题,请参考以下文章

Excel vba 多个条件查询应该怎么做,可以用find方法吗?

请教,在Excel 中使用VBA查找问题。

如何解决 excel vba 中的 Find(what:=) 字符限制

Excel VBA Range.FindNext v Range.Find - 我错过了啥?

Excel vba:.find 函数返回运行时错误 91

vba语言中find 的用法