首页 > 解决方案 > 循环并使用通配符匹配字符串

问题描述

我正在尝试遍历 A 列中的单元格并找到特定的文本字符串。文本字符串是这样的:

tag:(1001)[EX==]
tag:(1002)[EX==]
tag:(1003)[EX==]
etc...

我想使用这样的通配符:

"tag:(1###)[EX??]"

3 ### 是任意 3 个数字,而 2 ?? 是任意 2 个单个字符。

但是,我也希望 [EX??] 位于这样的数组中:

ArrTagSuffix = Array("[EX??]", "[IN??]", "[EXLW]")

所以...通配符字符串看起来像这样:

"tag:(1###)" & ArrTagSuffix

另外,我希望“tag:(1###)”中的“1”类似于“i”,这样我就可以像这样索引它:

"标签:(" & i & "###)" & ArrTagSuffix

我的第一个问题是这种带有通配符的格式似乎不起作用。我的第二个问题围绕着试图找出循环/搜索。

基本上,假设“i”可以是 1 到 6,并且 ArrTagSuffix 将有 3 个不同的字符串。最终可以有 18 种不同的“i's”和 ArrTagSuffix 组合。我想搜索A列...如果找不到匹配项...什么也不做...但是如果找到匹配项...将所有匹配的单元格复制到新工作表上。但我需要它将它复制到与找到的相同的单元格中...所以...例如...如果在单元格 A23 中找到匹配项...需要将其复制到新工作表的单元格 A23 .

所以......例如......如果Sheet1...... A列有:

Blank cell
Blank cell
tag:(1001)[EX==]
Blank cell
tag:(1002)[EX==]
tag:(1003)[EX==]
Blank cell
tag:(3001)[EX==]
tag:(3002)[EX==]
tag:(3003)[EX==]
tag:(6001)[IN==]
Blank cell
tag:(6002)[IN==]
tag:(6003)[IN==]
tag:(1001)[EXLW]
Blank cell
tag:(1002)[EXLW]
tag:(1003)[EXLW]

该程序将找到 4 个匹配项:

"1" and an "[EX??]"
"3" and an "[EX??]"
"6" and an "[IN??]"
"1" and an "[EXLW]"

所以...它会将 A 列的匹配项复制到 4 个新工作表的相应单元格中。

最初,我有一个更简单的任务,我只需要找到“标签”这个词,并且我有这个有效的代码:

With ActiveSheet
    Set criteriarange = Range("A1:A" & LShtRow)
        For Each criteriacell In criteriarange
            If Not criteriacell.Value Like "tag:*" Then
                criteriacell.ClearContents
            End If
        Next criteriacell
End With

所以我开始修改它以尝试完成这项任务......但就像我上面说的......我什至无法让通配符部分正常工作......更不用说循环......这是我的地方我在:

With ActiveSheet
    Set criteriarange = Range("A1:A" & LShtRow)
        For Each criteriacell In criteriarange
            If Not criteriacell.Value Like "tag:(" & i & "###)" & ArrTagSuffix Then
                criteriacell.ClearContents
            End If
        Next criteriacell
End With

我立即意识到它不知道如何处理“i”所以我暂时将“i”更改为“1”以查看是否有任何匹配项:

With ActiveSheet
    Set criteriarange = Range("A1:A" & LShtRow)
        For Each criteriacell In criteriarange
            If Not criteriacell.Value Like "tag:(" & "1" & "###)" & ArrTagSuffix Then
                criteriacell.ClearContents
            End If
        Next criteriacell
End With

但我没有向我表明我的通配符格式不正确。为了真正简化事情......我试过:

With ActiveSheet
    Set criteriarange = Range("A1:A" & LShtRow)
        For Each criteriacell In criteriarange
            If Not criteriacell.Value Like "tag:(1###)" & ArrTagSuffix Then
                criteriacell.ClearContents
            End If
        Next criteriacell
End With

仍然不起作用,我尝试了这个:

With ActiveSheet
    Set criteriarange = Range("A1:A" & LShtRow)
        For Each criteriacell In criteriarange
            If Not criteriacell.Value Like "tag:(1???)" & ArrTagSuffix Then
                criteriacell.ClearContents
            End If
        Next criteriacell
End With

仍然不起作用......并且不起作用,我的意思是它会清除单元格的内容,即使它们与我正在搜索的格式匹配。

标签: excelvbaloopsmatch

解决方案


正如我昨天评论的那样,Regex 是一个很好的工具。

首先,您需要在工具菜单中添加对 Microsoft VBScript 正则表达式的引用。

然后添加此代码:

Private Sub simpleRegex()
    Dim strPattern As String
    Dim regEx As New RegExp
    Dim strInput As String
    Dim Myrange As Range

    strPattern = "tag:\((\d)\d{3}\)\[(\w{2}.{2})\]"

    Set Myrange = ThisWorkbook.Sheets("Sheet1").Range("A1:A18")
    With regEx
        .Global = True
        .IgnoreCase = False
        .Pattern = strPattern
    End With

    If strPattern <> "" Then
        For Each c In Myrange
            strInput = c.Value
            If regEx.Test(strInput) Then
                Set matches = regEx.Execute(strInput)
                c.Offset(0, 1).Value = matches.Item(0).SubMatches.Item(0)
                c.Offset(0, 2).Value = matches.Item(0).SubMatches.Item(1)
            End If
        Next c
    End If
End Sub

代码将遍历 A1:A18 并查找模式,如果匹配,它将输出 B 列中的第一个数字和[]C 列中的代码。

在此处输入图像描述

然后,如果您只想要唯一的项目,则使用 excel 函数删除“数据”选项卡中的重复项,并仅勾选 B 列和 C 列。
这给出了:
在此处输入图像描述


推荐阅读