亲爱的,
我需要从单个单元格中拆分一个大字符串,没有好的分隔符。它是一个逐点点的'网球比赛的日期,从第三方软件直接导出到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)
重要提示:第一个真实点[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?
答案 0 :(得分:0)
根据您提供的示例,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仅检查A2
的单元格Worksheet(13)
中的文本。您可以进一步修改它以查看范围内的所有单元格