簡體   English   中英

Excel VBA:拆分沒有良好分隔符的大字符串

[英]Excel VBA: Splitting a big string with no good delimiters

親愛的大家,

我需要從一個單元格中拆分一個大字符串,沒有很好的定界符。 這是網球比賽中的“逐點”日期,可以直接從第三方軟件導出到Excel工作簿中。

不幸的是,我對VBA語言的了解不足以自己解決這個問題,因此在論壇中找不到類似的示例。 那么,有福的靈魂可以幫助我嗎?

這是我的A1單元格內容的一個示例:

0-0 [*0-0] [0-15*] [15-15*] [15-30*] [30-30*] [40-30*] [40-40*] [40-A*] [40-40*] [A-40*] 1-0 [*0-0] [*0-15] [*15-15] [*15-30] [*30-30] [*40-30] 2-0 [0-0*] [15-0*] [30-0*] [30-15*] [40-15*] 3-0 [*0-0] [*0-15] [*15-15] [*30-15] [*40-15] 4-0 [0-0*] [15-0*] [30-0*] [40-0*] 5-0 [*0-0] [*15-0] [*15-15] [*30-15] [*40-15] 6-0 0-0 [0-0*] [0-15*] [0-30*] [0-40*] 6-0 0-1 [*0-0] [*0-15] [*15-15] [*15-30] [*30-30] [*30-40] [*40-40] [*A-40] 6-0 1-1 [0-0*] [0-15*] [15-15*] [30-15*] [30-30*] [40-30*] 6-0 2-1 [*0-0] [*15-0] [*15-15] [*15-30] [*30-30] [*40-30] [*40-40] [*A-40] [*40-40] [*A-40] [*40-40] [*40-A] 6-0 2-2 [0-0*] [0-15*] [0-30*] [15-30*] [15-40*] 6-0 2-3 [*0-0] [*0-15] [*0-30] [*0-40] 6-0 2-4 [0-0*] [0-15*] [0-30*] [0-40*] 6-0 2-5 [*0-0] [*15-0] [*30-0] [*30-15] [*40-15] 6-0 3-5 [0-0*] [0-15*] [0-30*] [15-30*] [30-30*] [40-30*] 6-0 4-5 [*0-0] [*15-0] [*30-0] [*40-0] 6-0 5-5 [0-0*] [0-15*] [15-15*] [30-15*] [30-30*] [30-40*] [40-40*] [40-A*] 6-0 5-6 [*0-0] [*15-0] [*30-0] [*30-15] [*40-15] [*40-30] 6-0 6-6 [0-0*] [*1-0] [*2-0] [2-1*] [3-1*] [*4-1] [*5-1] [6-1*] 6-0 7-6(1)
  • *表示正在服務的人。
  • 方括號內的數字是每局比賽或搶七局中的分數。
  • 方括號外的數字是每場比賽的最終分數。
  • 第一組(6-X或7-5)結束后,方括號外的數字包括先前設置的分數。

重要信息:在第一個實點[0-15*]之前的第一個字符是無用的IMO。 首先,因為通常指出誰在服務是錯誤的(例如在此示例中); 其次,因為有時字符串的開頭有些不同,沒有第一個"0-0"或其他一些無用的零,例如"0-0 [0-0] [* 0-0]"

就是說,我需要從這些數據中提取的只是兩件事:

  • 一列說明誰在第一場比賽中服役(左球員或右球員);
  • 僅游戲得分的順序(不逐點)在不同列中;

像這樣:

*1-0 | 1-1 | 2-1 | 3-1 | 4-1 ...*

我已經使用Excel公式進行了此操作,但是我需要數十個新列,每個列都包含無效的公式,這使得無法在Excel中進行處理。

使用VBA Excel有最簡單的方法嗎? 我是否必須使用其他軟件或語言,例如R或Power Bi?

根據您提供的示例,UDF下面將為您提供一個包含設置詳細信息的數組( aGameScore )。 數組的元素數與字符串中列出的集合數相對應。 每個元素都以“ Left Player或“ Right Player開頭:指示集合中哪個球員先發球。 之后,每個數組元素保存字符串中列出的每個游戲的得分

Sub GetScores()

    Dim aScores As Variant
    Dim aGameScore As Variant
    Dim iC&, iHyphLoc&, iServerLoc&
    Dim sServer$

    With ThisWorkbook.Worksheets("Sheet13")

        aScores = Split(.Range("A2"), " ")

        For iC = LBound(aScores) To UBound(aScores)
            If InStr(1, aScores(iC), "0-0") > 0 And InStr(1, aScores(iC), "[") = 0 Then
                ' Set who sereved in the first game
                iC = iC + 1
                If IsArray(aGameScore) Then
                    ReDim Preserve aGameScore(UBound(aGameScore) + 1)
                Else
                    ReDim aGameScore(0)
                End If
                iHyphLoc = InStr(1, aScores(iC), "-")
                iServerLoc = InStr(1, aScores(iC), "*")
                If iHyphLoc > iServerLoc Then
                    aGameScore(UBound(aGameScore)) = "Left Player"
                Else
                    aGameScore(UBound(aGameScore)) = "Right Player"
                End If
            ElseIf InStr(1, aScores(iC), "[") = 0 Then
                ' Capture game scores
                If iC = UBound(aScores) Then
                    aGameScore(UBound(aGameScore)) = aGameScore(UBound(aGameScore)) & " | " & Trim(aScores(iC))
                ElseIf InStr(1, aScores(iC + 1), "[") <> 0 Then
                    aGameScore(UBound(aGameScore)) = aGameScore(UBound(aGameScore)) & " | " & Trim(aScores(iC))
                End If
            End If
        Next

    End With

End Sub

目前,UDF僅檢查Worksheet(13)單元格A2中的文本。 您可以進一步修改它以查看您范圍內的所有單元格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM