在VBA中,我正在尝试构建一个可以像这样转换字符串的通用函数:
a)=IFERROR(PERCENTRANK($FU$23:$FU$2515,FU24,3)*100,FY$17)
b)=IF(FZ$16=(BDP($C24,FZ$18,FZ$19,"EQY_FUND_CRNCY",FX)),FZ$17,IF($B24="","",BDP($C24,FZ$18,FZ$19,"EQY_FUND_CRNCY",FX)))
c)=IF(ISNUMBER(FU24),TRUNC((((COUNTIF($J$23:$J$2515,$J24)-(SUMPRODUCT(($J$23:$J$2515=$J24)*(FU24<FU$23:FU$2515))))/COUNTIF($J$23:$J$2515,$J24)))*100,2),FX$17)
d)=IFERROR(PERCENTRANK(EO$23:EO$2515,EO24,3)*(-100)+100,ET$17)
e)=BDP($C24,EH$18,EH$19,"EQY_FUND_CRNCY",FX)
进入这些:
a)23 2515 24 17
b)16 24 18 19 17 24 24 18 19
c)24 23 2515 24 23 2515 24 24 23 2515 23 2515 24 17
d)23 2515 24 17
e)24 18 19
换句话说,删除除了单元格引用行之外的所有内容,并用空格(或其他一些分隔符)将它们分开,这样我以后就可以VBA.split(x," ")
。
注意:
我构建了这个可能有用的小测试子(我不想做我想做的事):
Sub test()
Dim s As String
s = "=IFERROR(PERCENTRANK($FU$23:$FU$2515,FU24,3)*100,FY$17)"
Dim s2 As String
Dim s3 As String
Dim s1 As String
Static re As RegExp
If re Is Nothing Then Set re = New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "[$]"
s1 = re.Replace(s, "")
re.Pattern = "[^A-Z0-9 ]"
s2 = re.Replace(s1, " ")
re.Pattern = "[^0-9]"
s3 = re.Replace(s2, " ")
Debug.Print s3
End Sub
答案 0 :(得分:3)
尝试:
Sub test()
Dim s As String, matches, m
s = "=IFERROR(PERCENTRANK($FU$23:$FU$2515,FU24,3)*100,FY$17)"
Static re As Object
If re Is Nothing Then
Set re = CreateObject("VBScript.RegExp") 'late binding
re.IgnoreCase = True
re.Global = True
re.Pattern = "[A-Z]+\$?(\d+)"
End If
Set matches = re.Execute(s)
If matches.Count > 0 Then
For Each m In matches
Debug.Print m.SubMatches(0)
Next m
End If
End Sub