VBA 正则表达式模式

问题描述 投票:0回答:1

我的一些数据看起来像这样,并在 CSV 文件中得到了它

1305-0023, "4. Appropriate_icf for you version embarrasing version O1_01_SEZ02_9-03, 29-Nov-2023", "Trialinning", "da-3aff-41fe-8292-28f03", "AMERICA", "Initiated", "SEZ6", 2024-07-22T20:46:13.364843Z, "01bf9039-d66a-470b-a403-b7d63c8271ec", "Upversion and downversion", "In Process by Sponsor", 2023-12-052024-09-09y35346282828, 400, "BRB BRB Approval Letter [Site]", "tehdueuehjjkksjbbcgdh-dheyeyehhehe-ebhdbhdhjdjdd", "Ongoing", NULL, 2024-09-09y35346282828

这里“”之间的数据是文本,没有的数据是其他字段。 我需要拆分数据并需要将其放入 Excel 的列中 请为我提供一个解决方案来相应地分割数据。

我尝试使用“,”进行拆分,但“”之间存在“,”,因此数据未正确拆分并得到错误结果。

excel vba csv
1个回答
0
投票

解析文本行

功能

Function ParsedTextLineToArray( _
    ByVal TextLine As String, _
    Optional ByVal ColumnDelimiter As String = ",") _
As Variant
    Const QUOTE_DELIMITER As String = """"

    Dim cLenI As Long: cLenI = Len(ColumnDelimiter) - 1
    
    Dim qSplit() As String: qSplit = Split(TextLine, QUOTE_DELIMITER)
    
    Dim qLimit As Long: qLimit = UBound(qSplit)
    
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    
    Dim q As Long, n As Long, c As Long, cStart As Long
    Dim cSplit() As String, cText As String, qText As String
    Dim WasDoubleQuoted As Boolean, IsFirstDone As Boolean
    
    For q = 0 To qLimit
        qText = qSplit(q)
        If WasDoubleQuoted Then
            n = n + 1
            dict(n) = qText
            WasDoubleQuoted = False
        Else
            If qText = ColumnDelimiter Then
                WasDoubleQuoted = True
            Else
                If InStrRev(qText, ColumnDelimiter) = Len(qText) - cLenI Then
                    WasDoubleQuoted = True
                End If
                cSplit = Split(qText, ColumnDelimiter)
                For c = cStart To UBound(cSplit) + WasDoubleQuoted
                    n = n + 1
                    dict(n) = cSplit(c)
                Next c
                If Not IsFirstDone Then
                    cStart = 1
                    IsFirstDone = True
                End If
            End If
        End If
    Next q
    
    ParsedTextLineToArray = dict.Items
    
End Function

测试

Sub Test()

    Const TEST_STRING As String = "1305-0023, " _
        & """4. Appropriate_icf for you version embarrasing version " _
        & "O1_01_SEZ02_9-03, 29-Nov-2023"", " _
        & """Trialinning"", " _
        & """da-3aff-41fe-8292-28f03"", " _
        & """AMERICA"", " _
        & """Initiated"", " _
        & """SEZ6"", " _
        & "2024-07-22T20:46:13.364843Z, " _
        & """01bf9039-d66a-470b-a403-b7d63c8271ec"", " _
        & """Upversion and downversion"", " _
        & """In Process by Sponsor"", " _
        & "2023-12-052024-09-09y35346282828, " _
        & "400, " _
        & """BRB BRB Approval Letter [Site]"", " _
        & """tehdueuehjjkksjbbcgdh-dheyeyehhehe-ebhdbhdhjdjdd"", " _
        & """Ongoing"", " _
        & "NULL, " _
        & "2024-09-09y35346282828"
    Const COLUMN_DELIMITER As String = ", "
    
    Debug.Print TEST_STRING & vbLf & vbLf
    Debug.Print Join(ParsedTextLineToArray(TEST_STRING, ", "), vbLf)

End Sub

测试结果

1305-0023
4. Appropriate_icf for you version embarrasing version O1_01_SEZ02_9-03, 29-Nov-2023
Trialinning
da-3aff-41fe-8292-28f03
AMERICA
Initiated
SEZ6
2024-07-22T20:46:13.364843Z
01bf9039-d66a-470b-a403-b7d63c8271ec
Upversion and downversion
In Process by Sponsor
2023-12-052024-09-09y35346282828
400
BRB BRB Approval Letter [Site]
tehdueuehjjkksjbbcgdh-dheyeyehhehe-ebhdbhdhjdjdd
Ongoing
NULL
2024-09-09y35346282828
© www.soinside.com 2019 - 2024. All rights reserved.