vba:regex,从公式string中除去所有单元格引用行引用

在VBA中,我试图build立一个通用函数来转换这样的string:

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," ")

笔记:

  1. 不属于单元格引用的数字将被删除。
  2. 要使用这个函数,你必须有正则expression式库。 如果下面的代码不起作用,请包括库: http : //support.microsoft.com/kb/818802 。 (注意:如果您知道如何将代码库包含在代码中,而不必遵循这些说明,请分享。)
  3. 这个例子中的公式列表只是一个例子。 我正在寻找一个通用的解决scheme。
  4. 我build立了这个小testing可能是有帮助的(它不是我想做的):

     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 

尝试:

 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 = "[AZ]+\$?(\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