0
我试图用正则表达式替换Excel中的子串“AV-”。我创建了如下因素的功能,但它没有找到或更换比赛使用Regex查找和替换Substing
Sub simpleRegex()
Dim strPattern As String: strPattern = "[AV-]{AV-}"
Dim strReplace As String: strReplace = "ZS-"
Dim regEx As New RegExp
Dim strInput As String
Dim Myrange As Range
Set Myrange = ActiveSheet.Range("E" & (ActiveCell.Row))
If strPattern <> "" Then
strInput = Myrange.Value
strReplace = ""
With regEx
.Global = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
ActiveSheet.Range("E" & (ActiveCell.Row)).Value = (regEx.Replace(strInput, strReplace))
Else
MsgBox ("Not matched")
End If
End If
末次
我假设你正在尝试学习一些东西,因为这可以用vba中的一行来完成'Myrange.Replace“AV-”,“ZS-”,xlPart,True' –
很难说什么是输入/输出的要求是,但你可以使用这样的东西 \t'ResultString = Regex.Replace(SubjectString,“(AV - )(。*)”,“ZS- $ 2-TRAILING”,RegexOptions.Multiline)'或什么斯科特已经提到。 – Greg