首页 > 解决方案 > 如何正确使用 Excel VBA 中的 Like 函数?

问题描述

我有 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----confi

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) 与列表项进行比较

...等等。

标签: excelvbacompare

解决方案


应该足以测试

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

为变量使用有意义的名称。使用带编号的变量名称,比如Arr2andArr3是你能做的最糟糕的事情。


编辑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

推荐阅读