简体   繁体   English

vba:regex,从公式字符串中删除除了单元格引用行引用之外的所有引用

[英]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: 笔记:

  1. Numbers that aren't part of a cell references are removed. 删除不属于单元格引用的数字。
  2. To use this function you must have the regular expression library. 要使用此功能,您必须具有正则表达式库。 If the code below doesn't work for you include the library: http://support.microsoft.com/kb/818802 . 如果以下代码不适合您,请包括库: http//support.microsoft.com/kb/818802 (Side note: if you know how to include the library in the code without having to follow those instructions, please share.) (旁注:如果您知道如何在代码中包含库而不必遵循这些说明,请分享。)
  3. The list of formulas in this example is just an example. 此示例中的公式列表只是一个示例。 I am looking for a generalized solution. 我正在寻找一个通用的解决方案。
  4. 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.

相关问题 使用正则表达式解析谷歌表格脚本中公式(先例)中的所有单元格引用 - Using RegEx to parse all cell references within a formula (precedents) in google sheets script 如何在 VBA 中编写 function 以从公式中提取单元格引用? - How can I write a function in VBA to extract cell references from a formula? 这是用于匹配 Excel 公式中的任何单元格引用的 RegEx 吗? - Is this the RegEx for matching any cell reference in an Excel formula? 使用Java中的regex提取excel中的所有单元格引用 - Extract all cell references in excel with regex in java 如何使用函数或正则表达式从单元格中删除所有数字? - How to remove ALL numbers from a cell with a function or regex? 正则表达式从电子表格公式中提取有效的单元格引用 - Regular expression to extract valid cell references from a spreadsheet formula 使用正则表达式从 XML 中删除命名空间引用 - Remove Namespace references from XML with Regex VBA正则表达式删除电子邮件地址以外的所有地址 - VBA regex to remove all but the email addresses 如何从单元格中删除多个文本字符串? - How to remove multiple text strings from a cell? 如何在非工作表引用的Google表格公式的所有部分加上引号 - How to put quotation marks around all parts of a Google Sheets formula that are not cell references
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM