代码正在工作,但它不循环每行以将答案放在其相邻列中。所有列B都得到最后一行的答案。正则表达式可以删除所有以某个字符串开头的东西,然后是其他东西?
Private Sub simpleRegex()
'Dim strPattern As String: strPattern = "^[0-9]{1,2}"
Dim strPattern As String: strPattern = "BSU:.*"
Dim strReplace As String: strReplace = ""
Dim regEx As New RegExp
Dim strInput As String
Dim Myrange As range
Dim r As range
Set r = ActiveSheet.range("A1", range("A1").End(xlDown))
Set Myrange = ActiveSheet.range("A1:A5")
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
r.Offset(0, 1).Value = (regEx.Replace(strInput, strReplace))
'MsgBox (regEx.Replace(strInput, strReplace))
Else
'MsgBox ("Not matched")
End If
End If
Next
End Sub
是否要保留或删除'BSU:'本身? – marekful
删除'BSU:'本身 – cookiemonster