excel - 使用 Excel 的 2 个字符串查找和搜索
问题描述
我需要你的帮助,
是否可以根据 2 个给定值而不是 1 个来搜索 Excel 电子表格?类似于“AND”运算符?
到目前为止,这是我所拥有的,但我想如果您在干草堆中寻找针:
Str = "apples" AND "oranges"
With Sheets(xSheet)
Set foundCell = .Cells.Find(What:=Str, After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
解决方案
下面的代码返回想要的结果:
Sub TestMe()
Dim str As String: str = "apples*oranges"
Dim foundCell As Range
Dim options As Variant
options = Array(str, Split(str, "*")(1) & "*" & Split(str, "*")(0))
Dim myVar As Variant
For Each myVar In options
With Worksheets(1)
Set foundCell = .Cells.Find(What:=myVar, After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
If Not foundCell Is Nothing Then Exit For
Next myVar
If Not foundCell Is Nothing Then
Debug.Print foundCell.Address
End If
End Sub
我们有两个不同的字符串需要搜索:"apples*oranges"
和"oranges*apples"
。拆分和反向操作非常原始:
options = Array(str, Split(str, "*")(1) & "*" & Split(str, "*")(0))
然后,使用 a和For Each Loop
early搜索两个字符串。Exit For
.Find()
推荐阅读
- android - 生成 APK 时出错,错误“程序类型已存在:android.support.v7.widget.DefaultItemAnimator$1”
- sql-server - Microsoft 自然语言列表:是否有“无语言”或“原始 unicode”或“语言中立”的等价物?
- c# - 同时在多个选项卡中加载同一页面
- powershell - 为什么 PowerShell 异常消息中的函数名称为空?如何正确获取?
- html - 如何设置溢出隐藏以便两边溢出,元素保持居中?
- angular - 角度重定向到身份验证
- android - How can i pass a variable from my adapter to a fragment i instantiate in Kotlin
- javascript - Socket.IO 中间件,io.use
- ios - 数据结构中的节点应该持有对对象的引用还是对象本身?
- node.js - Node.js WebdriverIO 错误