[英]vba: regex, remove all but cell reference row references from formula strings
In VBA I am trying to build a generalized function that turn strings like these: 在VBA中,我试图构建一个通用函数来转换这些字符串:
a) =IFERROR(PERCENTRANK($FU$23:$FU$2515,FU24,3)*100,FY$17)
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)))
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)
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)
d)
=IFERROR(PERCENTRANK(EO$23:EO$2515,EO24,3)*(-100)+100,ET$17)
e) =BDP($C24,EH$18,EH$19,"EQY_FUND_CRNCY",FX)
e)
=BDP($C24,EH$18,EH$19,"EQY_FUND_CRNCY",FX)
Into these: 进入这些:
a) 23 2515 24 17
a)
23 2515 24 17
b) 16 24 18 19 17 24 24 18 19
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
c)
24 23 2515 24 23 2515 24 24 23 2515 23 2515 24 17
d) 23 2515 24 17
d)
23 2515 24 17
e) 24 18 19
e)
24 18 19
In other words, remove everything except cell reference rows and separate them with spaces (or some other deliminator) so I can VBA.split(x," ")
them later. 换句话说,删除除了单元格引用行之外的所有内容,并用空格(或其他一些分隔符)将它们分开,以便
VBA.split(x," ")
我可以使用VBA.split(x," ")
。
Notes: 笔记:
I built this little test sub that might be helpful (IT DOESN'T DO WHAT I WANT): 我建立了这个可能有用的小测试子(我不想做我想做的事):
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
Try: 尝试:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.