[英]How do I use the Like Function in Excel VBA properly?
我有 2 个数组,一个包含文件路径,1 个包含关键术语,如果它们与文件路径匹配,则应该执行操作。 我无法让 2 个数组匹配,我正在尝试使用通配符。
For d = 0 To C
If arr2(d) <> "" Then
For i = LBound(Arr3) To UBound(Arr3)
If (Arr3(i) <> "") And ("*" & Arr3(i) & "*") Like ("*" & arr2(d) & "*") Then
MsgBox arr2(d) & "----" & Arr3(i)
End If
Next
End If
Next
我已经移动了消息框以在它们进入 IF 语句之前检查这些术语,这只会让我更加困惑。 这是应该产生匹配的 3 个数组条目的 msgbox 文本,确定吗?!
MBappform.txt----appform
selfdec.txt----selfdec
机密性.txt----配置文件
I've tried this with and without double ASTERISK statements, I've tried one of each with ASTERISK statements.
I've got no idea why this isn't working.
任何帮助表示赞赏
编辑:
For d = 0 To C
If arr2(d) <> "" Then
For i = LBound(Arr3) To UBound(Arr3)
MsgBox "Do these Match? " & arr2(d) & " = " & Arr3(i)
& vbCrLf &
(Arr3(i) Like "*" & arr2(d) & "*")
If Arr3(i) Like "*" & arr2(d) & "*" Then
MsgBox "Works"
End If
Next
End If
Next
也许如果我提供更多背景信息,因为这仍然无法正常工作。 arr2(D) 是目录中文件名的数组,C 是目录中文件数量的计数。
对于 arr2 中的每个文件,我想交叉引用 arr3 中的一组关键术语。
因此,对于每个文件,这应该交叉引用编号。 arr2 的 1 和 arr3 的 8,那么它应该交叉引用 arr2 的 2 号和 arr3 的 8 号。
无论出于何种原因,我列出的字符串似乎都没有提供匹配项,例如 appform vs MBappform.txt 没有提供匹配项。
我同意命名不当的变量没有混淆。
伪代码会是这样的......
循环所有列表 1 对于活动列表 1 元素 - 它是否匹配任何列表 2 元素
列表 1.item1 是否等于任何列表 2 如果它是然后保存列表 2 项目以备后用,如果不是什么都不做,
将所有列表 2 项与第一个列表 1 项进行比较后,将下一个列表 1 项 (item2) 与列表项进行比较
...等等。
测试应该够了
If Arr3(i) Like "*" & Arr2(d) & "*" Then
没有必要测试Arr3(i) <> ""
因为它不匹配"*" & arr2(d) & "*"
如果它是空的。
星号仅在Like
运算符之后使用。
您可以轻松地测试它
MsgBox "MBappform.txt" Like "*" & "appform" & "*"
这会给你True
。
编辑:
看起来你混淆了你的变量:
If Arr2(i) Like "*" & Arr3(d) & "*" Then
为变量使用有意义的名称。 使用编号的变量名称(如Arr2
和Arr3
是最糟糕的做法。
编辑2:
请查看以下示例。 通过这个例子,你应该能够修复你的代码:
Option Explicit
Public Sub FindKeywordsInFileNames()
Dim ArrFileNames As Variant
ArrFileNames = Array("MBappform.txt", "selfdec.txt", "confidentiality.txt", "SomeRandomFile.txt", "AnotherFile.txt")
'instead of the above code retrieve your file names
Dim ArrKeyWords As Variant
ArrKeyWords = Array("appform", "selfdec", "confi")
'the keywords you are looking for in the filenames
Dim FileName As Variant
For Each FileName In ArrFileNames
Dim KeyWord As Variant
For Each KeyWord In ArrKeyWords
If FileName Like "*" & KeyWord & "*" Then
'key word is in the file name
Debug.Print FileName, KeyWord, "True"
Else
'key word is not in the file name
Debug.Print FileName, KeyWord, "False"
End If
Next KeyWord
Next FileName
End Sub
立即窗口中的输出应类似于:
MBappform.txt appform True
MBappform.txt selfdec False
MBappform.txt confi False
selfdec.txt appform False
selfdec.txt selfdec True
selfdec.txt confi False
confidentiality.txt appform False
confidentiality.txt selfdec False
confidentiality.txt confi True
SomeRandomFile.txt appform False
SomeRandomFile.txt selfdec False
SomeRandomFile.txt confi False
AnotherFile.txt appform False
AnotherFile.txt selfdec False
AnotherFile.txt confi False
编辑 3
如果您没有文件名数组(正如您在问题开头所说的那样),那么您需要另一个循环与Dir()
一起使用:
Option Explicit
Public Sub FindKeywordsInFileNames()
Dim ArrKeyWords As Variant
ArrKeyWords = Array("appform", "selfdec", "confi")
'the keywords you are looking for in the filenames
Dim FileName As String 'initialize folder (and first file name)
FileName = Dir("C:\Temp\")
Do While FileName <> vbNullString
Dim KeyWord As Variant
For Each KeyWord In ArrKeyWords
If FileName Like "*" & KeyWord & "*" Then
'key word is in the file name
Debug.Print FileName, KeyWord, "True"
Else
'key word is not in the file name
Debug.Print FileName, KeyWord, "False"
End If
Next KeyWord
FileName = Dir() 'next filename
Loop
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.