[英]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 ![]() |
Another header![]() |
Project names![]() |
---|---|---|
First![]() |
row1![]() |
AA_Bla_ ABCDEF ![]() |
Second![]() |
Blah![]() |
XY_Blah_ ABCDEF ![]() |
Fourth![]() |
Again this project name![]() |
AA_Bla_ ABCDEF ![]() |
Third![]() |
Blubb![]() |
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
中搜索满足特定条件的所有条目。
startPattern = "(^[AZ]{2})"
projectPattern = "(.$){6}"
Therefore, I thought of regEx and tried: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.