我认为正则表达式或 Regexp 是您正在寻找的。
以下模式
([A-Z0-9]*)!(\${0,1})([A-Z]{1,3})(\${0,1})([0-9]*)
将匹配“Sheet1!A1”、“Sheet1!$A$1”、“Sheet1!$A1”、“Sheet1!A$1”之类的任何内容
解释:
([A-Z0-9]*)! = Find anything that is before "!"
(\${0,1}) = $ or nothing
([A-Z]{1,3}) = between one and three letters
([0-9]*) = Any number
您应该能够轻松地修改该模式以仅匹配您想要的。特别是 ([A-Z0-9]*)!(\${0,1})B(\${0,1})1,只会匹配其中包含 B($)1 的内容...使用字符串操作构造正则表达式模式,应该很好。
您需要参考(工具 > 参考)“Microsoft VBScript 正则表达式 5.5”
尝试以下代码,这应该为您提供实现目标的所有工具
Sub ReplaceReference()
' Reference: Microsoft VBScript Regular Expressions 5.5
Dim RegEx As Object
Set RegEx = New RegExp
Dim s As String
' Here I have hardcoded the reference to the original cell for demonstration purposes
s = "Sheet1!$AB$2"
' Replacement: New sheetname, New Column, new row number
Dim NewCol As String, NewRow As String
NewCol = "C"
NewRow = "10"
Dim NewSheet As String
NewSheet = "Sheet2"
With RegEx
.Pattern = "([A-Z0-9]*)!(\${0,1})([A-Z]{1,3})(\${0,1})([1-9]*)"
.IgnoreCase = True
.Global = True
End With
Debug.Print RegEx.Replace(s, NewSheet & "!" & "$2" & NewCol & "$4" & NewRow)
End Sub
干杯,朱利安