/ / Ako môžem vytlačiť moju extrahovanú vzorku v stĺpci pomocou regex.execute a zhodu objektu v vba? - regex, excel, vba, excel-vba

Ako môžem vytlačiť môj extrahovaný vzor v stĺpci pomocou regex.execute a zhody objektu v vba? - regex, excel, vba, excel-vba

Používam vba, aby som napísal sub, aby som získal kódy pinovz danej adresy v stĺpci v pracovnom hárku programu Excel. Bol som schopný nájsť regex vzor extrahovať kolík vzor, ​​ale Im nemôže vyvodiť uvedené extrahované kolíky do stĺpca. Ako spôsob, ako otestovať, či je regex schopný extrahovať vzor kolíka zo stĺpca (je to), prešiel som vlastnosť Match.value z objektov zhody na msgbox a dokázal získať výstup pre každý reťazec v msgbox.

Private Sub simpleRegex()
Dim strPattern As String: strPattern = "d{6}"
Dim Match As Object
Dim matches As Object

Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
Dim strInput As String
Dim Myrange As Range

Set Myrange = ActiveSheet.Range("B1:B30")

For Each cell In Myrange
If strPattern <> "" Then
strInput = cell.Value

With regex
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With

If regex.Test(strInput) Then
Set matches = regex.Execute(strInput)
For Each Match In matches
MsgBox (Match.Value) "A workaround I found to see if my pattern
"worked but I need to print Match.value
"in a column so this wont do
Next
Else
MsgBox ("Not matched")
End If
End If
Next
End Sub

Ako extrahovať vzorový reťazec z objektu zhody a vytlačiť ho do stĺpca (ako U1: U30) pre každú bunku v mojom rozsahu B1: B30

TL; DR: Regex Pattern pracuje, ale ako vytlačiť extrahovaný vzor v bunke

odpovede:

0 pre odpoveď č. 1

Čo sa týka zhromažďovania čiar oddelených v reťazci strMatches a napíšte to do bunky?


Pridajte to predtým For Each cell In Myrange

Dim i As Long, strMatches As String
i = 1 "row number where we start to write

A nahraďte svoj druhý For Each s

strMatches = vbNullString
For Each Match In matches
strMatches = strMatches & Match.Value & ", " "collect all matches comma seprated
Next
If Not strMatches = vbNullString Then strMatches = Left(strMatches, Len(strMatches) - 2) "remove last comma
Worksheets("your-sheet-name").Range("U" & i).Value = strMatches "write the matches into cell
i = i + 1