示例代码如下:
Sub RegExpDemoReplace() Dim Res() Dim objRegEx As Object Dim objMH As Object Dim j As Integer Set objRegEx = CreateObject("vbscript.regexp") objRegEx.Pattern = "[^!]([A-Z]{1,3}\d{1,6})" objRegEx.Global = True For i = 1 To Cells(Rows.Count, "F").End(xlUp).Row form = " " & Cells(i, "F") pre = Cells(i, 1) & "!" Set objMH = objRegEx.Execute(form) If objMH.Count > 0 Then newform = form For j = 0 To objMH.Count - 1 ref = objMH(j).submatches(0) newform = Replace(newform, ref, pre & ref) Next Cells(i, "I") = Trim(newform) End If Next Set objRegEx = Nothing Set objMH = NothingEnd Sub