excel - “查找”命令的表达式
问题描述
我想使用“查找”命令一次搜索一个单元格,而不是一次搜索一个单元格范围。我尝试使用如下所示的“For Each”命令,但它不起作用。是否可以使用其他表达式搜索单词,例如使用“范围”Set rgFound = activecell.Find("John")
或 “范围”?Set rgFound = cells(1,1).Find("John"))
Sub tester()
Dim rgFound As Range
Set rng = Columns(1)
Set rng1 = rng.SpecialCells(xlVisible)
For Each cell In rng1
Set rgFound = cell.Find("John")
If rgFound Is Nothing Then
MsgBox "Name was not found."
Else
MsgBox "Name found in :" & rgFound.Address
End If
Next
End Sub
解决方案
要搜索单个单元格的值,您必须使用InStr 函数
If InStr(1, Range("A1").Value, "John", vbTextCompare) > 0 Then
'John was found
Else
'John was not found
End If
InStr
返回在单元格 A1 的值中找到的第一个位置John
,所以如果它返回一个数字,> 0
那么它就没有找到。
或者,您可以使用Like 运算符:
If Range("A1").Value Like "*John*" Then
'John was found
Else
'John was not found
End If
注意周围的占位符(*
星号),以确保类似的操作员John
在"any string that conains John in the begining middele or end of the string"
推荐阅读
- python - TypeError:“生成器”对象不可下标错误
- python - 连接数据帧,重视来自特定数据帧的重复行
- neo4j - 在节点内存储对象数组 - Neo4j
- javascript - [Vue 警告]:v-on 处理程序中的错误:“ReferenceError:fb 未定义”
- javascript - ajax响应后如何刷新jquery中的表单
- node.js - 在 node.js 中使用 response.download() 后删除文件?
- amazon-web-services - create-ami 命令是否支持 EBS 支持的实例的 sshkey?
- android - 如何在点击文本时更改文本的颜色和/或装饰
- ios - React Native:从 CameraRoll.saveToCameraRoll 解码图像数据时出错
- node.js - 在 npm 启动时失败