VBA 提取字符串中的所有字段名

Jou*_*zer 3 excel vba

我有一个这样的字符串:

"'where CAST(a.DT_NPE_SORTIE as integer ) < cast (add_months (cast (a.dt_nep_restructuration as date format 'YYYYMMDD'), 12) as integer) and a.DT_NPE_SORTIE is not null and a.DT_NPE_SORTIE <> '99991231' and a.dt_npe_restructuration is not null and a.dt_npe_restructuration <> '99991231'"

我需要提取所有"a.FIELDNAME"喜欢a.dt_nep_restructurationa.DT_NPE_SORTIE从以前的屏幕。

我需要在 VBA 中为工作中的项目执行此操作。

到目前为止,我使用If&InStr来检查字符串中是否存在值列表​​。但是对我来说提取所有 a.FIELDNAME 然后检查它们是否与数组中的 fieldname 匹配会更容易。

此致,

约夫策尔

Sid*_*out 5

我看到有很多带有“NPE”和一种带有“NEP”?这是一个错字吗?如果是,那么它是否总是以 a.dt_nep_... 开头?– Siddharth Rout 9 分钟前

不,这是一个错字,以便为我的 vba 函数引发错误。它总是以“a”开头。– Jouvzer 6 分钟前

我处理过 NPE/NEP。这是你正在尝试的吗?

Option Explicit

Private Sub simpleRegex()
    Dim strPattern As String: strPattern = "a.dt_(nep|npe)_\w+"
    Dim regEx As Object
    Dim strInput As String
    Dim inputMatches As Object
    Dim i As Long
    
    strInput = "'where CAST(a.DT_NPE_SORTIE as integer ) < cast (add_months (cast (a.dt_nep_restructuration as date format 'YYYYMMDD'), 12) as integer) and a.DT_NPE_SORTIE is not null and a.DT_NPE_SORTIE <> '99991231' and a.dt_npe_restructuration is not null and a.dt_npe_restructuration <> '99991231'"
        
    Set regEx = CreateObject("VBScript.RegExp")
        
    With regEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = strPattern
    End With
        
    Set inputMatches = regEx.Execute(strInput)
    
    If inputMatches.Count <> 0 Then
        For i = 0 To inputMatches.Count - 1
            Debug.Print inputMatches.Item(i)
        Next i
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

注意:如果它以开头,a.dt那么你也可以使用a.dt_\w+_\w+