excel 利用正則表達式匹配工作表中的數據


excel 利用正則表達式匹配工作表中的數據

sheet1中A列放需要匹配的數據

sheet2中A列放正則表達式,可以是多個,但至少一個

匹配到了則在sheet1中C列對應行顯示相關標記內容,比如本案例中顯示1111

Sub Test()
atr = Worksheets("Sheet1").Range("a65536").End(xlUp).Row
btr = Worksheets("Sheet2").Range("a65536").End(xlUp).Row

a = Worksheets("Sheet1").Range("a1:a" & atr).Value
b = Worksheets("Sheet2").Range("a1:a" & btr).Value

ReDim c(1 To atr, 1 To 1)
Set reg = CreateObject("vbscript.regexp")
With reg
.Global = True
.IgnoreCase = True
For ar = 1 To atr
For br = 1 To btr
If btr = 1 Then
.Pattern = b
Else
.Pattern = b(br, 1)
End If
If .Test(a(ar, 1)) Then
c(ar, 1) = "1111"
Exit For
End If
Next
Next
End With
Range("c1:c" & atr) = c
Set reg = Nothing
End Sub

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM