vba 正则表达式用法

2023-02-17,,

Sub Regexp_test(Sht As String, str As String)
On Error Resume Next
Dim regx As Object
Dim arr, brr, mh
Set regx = CreateObject("vbscript.regexp")

With regx
  .Global = True

  '此处填入正则表达式
  .Pattern = ""

End With

With Sheets(Sht)

arr = .Range("C2:C" & .[A65536].End(xlUp).Row)

ReDim brr(1 To UBound(arr), 1 To 1)
For i = 1 To UBound(arr)
  Set mh = regx.Execute(arr(i, 1))

  If mh(0).submatches(1) Like "*-*月*" Then
    brr(i, 1) = VBA.Mid(mh(0).submatches(1), (VBA.InStr(mh(0).submatches(1), "月") + 1), (VBA.InStr(mh(0).submatches(1), "_") - 1))
    ElseIf mh(0).submatches(1) Like "*_*" Then
    brr(i, 1) = VBA.Left(mh(0).submatches(1), VBA.InStr(mh(0).submatches(1), "_") - 1)

  Else
    brr(i, 1) = mh(0).submatches(1)
  End If
Next i

End With
On Error GoTo 0
End Sub

vba 正则表达式用法的相关教程结束。

《vba 正则表达式用法.doc》

下载本文的Word格式文档,以方便收藏与打印。