简体   繁体   English

VBA 在数组中查找所有正则表达式匹配项

[英]VBA Find all regEx matches in array

I have a large list and want to find all entries for the same project names.我有一个很大的列表,想要找到相同项目名称的所有条目。

My data looks like this:我的数据如下所示:

A header header Another header另一个header Project names项目名称
First第一的 row1第 1 行 AA_Bla_ ABCDEF AA_Bla_ABCDEF
Second第二 Blah废话 XY_Blah_ ABCDEF XY_Blah_ ABCDEF
Fourth第四 Again this project name又是这个项目名称 AA_Bla_ ABCDEF AA_Bla_ABCDEF
Third第三 Blubb布鲁布 12_Blubb_ABCDEF 12_Blubb_ABCDEF

Therefore, I have this code, which gets all the possible filter criteria (Project names):因此,我有这段代码,它获取所有可能的过滤条件(项目名称):

lastRow = Range(CStr("C" & ActiveSheet.Rows.Count)).End(xlUp).Row

Dim data(), dict As Object, r As Long
Set dict = CreateObject("Scripting.Dictionary")

data = ActiveSheet.Range("C2", "C" & CStr(lastRow)).Columns(1).Value

For r = 1 To UBound(data)
    dict(data(r, 1)) = Empty
Next

data = WorksheetFunction.Transpose(dict.keys())
End Sub

I can access the list of project names like:我可以访问项目名称列表,例如:

Debug.Print data(1, 1) ' AA_Bla_ABCDEF
Debug.Print data(2, 1) ' XY_Blah_ABCDEF  
Debug.Print data(3, 1) ' 12_Blubb_ABCDEF 

Now, I would like to search in data for all entries that fulfill certain criteria.现在,我想在data中搜索满足特定条件的所有条目。

  1. I want to exclude all items that do not start with letters.我想排除所有不以字母开头的项目。 startPattern = "(^[AZ]{2})"
  2. I want to find in all remaining items those who have the same last 6 symbols (numbers, chars, underscores...) projectPattern = "(.$){6}" Therefore, I thought of regEx and tried:我想在所有剩余的项目中找到那些具有相同最后 6 个符号(数字、字符、下划线......) projectPattern = "(.$){6}"因此,我想到了 regEx 并尝试了:
Dim regEx As Object
Set regEx = CreateObject("VBScript.RegExp") ' Automatic reference binding

    For r = 1 To UBound(data)

        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = projectPattern 
        End With

    ' If data.find(regEx).count > 1 (if I have this pattern more than once)
         ' similarEntries = data.find(regEx) ...

How can I search the array for all matches that occur more than once?如何在数组中搜索多次出现的所有匹配项? In the example list it would be only: AA_Bla_ ABCDEF在示例列表中,它只会是:AA_Bla_ ABCDEF

Using LIKE "[AZ][AZ]" to exclude some items and RIGHT(string,6) as dictionary key to count duplicates.使用LIKE "[AZ][AZ]"排除某些项目并使用RIGHT(string,6)作为字典键来计算重复项。

Option Explicit

Sub Macro1()

    Dim ws As Worksheet
    Dim dict As Object, name As String, key, ar
    Dim r As Long, lastrow As Long
   
    Set ws = ActiveSheet
    Set dict = CreateObject("Scripting.Dictionary")
   
    lastrow = ws.Cells(Rows.Count, "C").End(xlUp).Row
    For r = 2 To lastrow
        name = Trim(ws.Cells(r, "C"))
        If UCase(Left(name, 2)) Like "[A-Z][A-Z]" Then
            key = Right(name, 6)
            If dict.exists(key) Then
                dict(key) = dict(key) & vbTab & name
            Else
                dict(key) = name
            End If

        End If
    Next

    ' show results on sheet2
    r = 1
    For Each key In dict
        ar = Split(dict(key), vbTab)
        If UBound(ar) > 0 Then
            Sheet2.Cells(r, 1) = key
            Sheet2.Cells(r, 2) = UBound(ar) + 1
            Sheet2.Cells(r, 3).Resize(1, UBound(ar) + 1) = ar
            r= r + 1
        End If
    Next
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM