Another challenging numbers extraction

AlanY

Well-known Member
Joined
Oct 30, 2014
Messages
4,528
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
This is a typical results for a table tennis league match that I’ve downloaded from the league and would like to do some analysis.


Excel 2013/2016
ABCD
1Home PlayerAway PlayerGamesScore
2AX7-1111-411-611-71-0
3AY11-811-611-61-0
4AZ11-711-86-117-1112-101-0
5BX9-1111-67-116-110-1
6BY11-911-811-91-0
7BZ8-1111-411-74-1110-120-1
8CX11-1311-76-119-110-1
9CY7-116-1111-313-119-110-1
10CZ8-115-1111-82-110-1
11ACXZ13-1511-48-1111-910-120-1
Sheet5


The results in Column-C are what I’m interested. The match is for best of 5 games (i.e. between 3-5 games) and each game to 11 points or 2 clear points if the score reached 10-10. E.g. the match between B & Z in Row 7 is 8-11, 11-4, 11-7, 4-11 and 10-12, Z won by 3-2.

The problem as you can see from the table is the score are all joined together with no spaces between them!
Any ideas as how to separate them would be much appreciated

From 7-1111-411-611-7 to 7-11 11-4 11-6 11-7 ?
 
Last edited:
tested on a few results and it works
I haven't yet thought of a fool-proof alternative, but by my reckoning that function fails for this set score

13-1119-211-114-1113-11

where it produces
13-11, 19-2, 11-11, 4-11, 13-11


instead of
13-11, 19-21, 1-11, 4-11, 13-11
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Initially, I thought of using Len(2) if after "-" or if the first number is 1, but there doesn't seem to be a catch all solution, especially for triplets of numbers.

Curious to what final solution is, I think it's too ambiguous with current information to clearly define a rule set.
 
Upvote 0
I think it's too ambiguous with current information to clearly define a rule set.
Well, I figured that I could do it manually so there should be a way. This 2-pass method is the best I could come up with.

Code:
Sub GetScores()
  Dim a As Variant, bits As Variant
  Dim i As Long, j As Long
  Dim s As String
  
  a = Range("C2", Range("C" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 5) As String
  For i = 1 To UBound(a)
    j = 0
    s = a(i, 1)
    Do While Len(s) > 0
      j = j + 1
      b(i, j) = Left(s, InStr(1, s, "-") + 2)
      s = Replace(s, b(i, j), "", 1, 1)
      If Left(s, 1) = "-" Then
        s = Right(b(i, j), 1) & s
        b(i, j) = Left(b(i, j), Len(b(i, j)) - 1)
      End If
    Loop
  Next i
  For i = 1 To UBound(a)
    For j = 1 To 5
      bits = Split(b(i, j), "-")
      If UBound(bits) = -1 Then Exit For
      Select Case False
        Case (bits(0) = 11 And bits(1) < 10) Or (bits(1) = 11 And bits(0) < 10) Or (Abs(bits(0) - bits(1)) = 2)
          If bits(0) < 11 Then
            b(i, j) = Right(b(i, j - 1), 1) & b(i, j)
            b(i, j - 1) = Left(b(i, j - 1), Len(b(i, j - 1)) - 1)
          End If
      End Select
    Next j
  Next i
  Range("E2").Resize(UBound(b), 5).Value = b
End Sub

Here is my test data and results


Book1
CDEFGHI
211-1311-76-119-1111-1311-76-119-11
311-131-1111-131-11
411-126-2411-126-24
57-1111-411-611-77-1111-411-611-7
611-811-611-611-811-611-6
711-711-86-117-1112-1011-711-86-117-1112-10
89-1111-67-116-119-1111-67-116-11
911-911-811-911-911-811-9
108-1119-211-114-1110-128-1119-211-114-1110-12
1111-1311-76-119-1111-1311-76-119-11
127-116-1111-313-119-117-116-1111-313-119-11
138-115-1111-82-118-115-1111-82-11
1413-1511-48-1111-910-1213-1511-48-1111-910-12
151-1118-2011-131-290-111-1118-2011-131-290-11
TT Scores (2)
 
Upvote 0
Well, I figured that I could do it manually so there should be a way. This 2-pass method is the best I could come up with.

Code:
Sub GetScores()
  Dim a As Variant, bits As Variant
  Dim i As Long, j As Long
  Dim s As String
  
  a = Range("C2", Range("C" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 5) As String
  For i = 1 To UBound(a)
    j = 0
    s = a(i, 1)
    Do While Len(s) > 0
      j = j + 1
      b(i, j) = Left(s, InStr(1, s, "-") + 2)
      s = Replace(s, b(i, j), "", 1, 1)
      If Left(s, 1) = "-" Then
        s = Right(b(i, j), 1) & s
        b(i, j) = Left(b(i, j), Len(b(i, j)) - 1)
      End If
    Loop
  Next i
  For i = 1 To UBound(a)
    For j = 1 To 5
      bits = Split(b(i, j), "-")
      If UBound(bits) = -1 Then Exit For
      Select Case False
        Case (bits(0) = 11 And bits(1) < 10) Or (bits(1) = 11 And bits(0) < 10) Or (Abs(bits(0) - bits(1)) = 2)
          If bits(0) < 11 Then
            b(i, j) = Right(b(i, j - 1), 1) & b(i, j)
            b(i, j - 1) = Left(b(i, j - 1), Len(b(i, j - 1)) - 1)
          End If
      End Select
    Next j
  Next i
  Range("E2").Resize(UBound(b), 5).Value = b
End Sub

Here is my test data and results


Book1
CDEFGHI
211-1311-76-119-1111-1311-76-119-11
311-131-1111-131-11
411-126-2411-126-24
57-1111-411-611-77-1111-411-611-7
611-811-611-611-811-611-6
711-711-86-117-1112-1011-711-86-117-1112-10
89-1111-67-116-119-1111-67-116-11
911-911-811-911-911-811-9
108-1119-211-114-1110-128-1119-211-114-1110-12
1111-1311-76-119-1111-1311-76-119-11
127-116-1111-313-119-117-116-1111-313-119-11
138-115-1111-82-118-115-1111-82-11
1413-1511-48-1111-910-1213-1511-48-1111-910-12
151-1118-2011-131-290-111-1118-2011-131-290-11
TT Scores (2)

many thanks Peter, much appreciated.
i first tried to tackle this problem last year and soon given up, thinking that if only they could put spaces in the results!

but, then I wouldn't be getting these gems out of you guys.
many thanks to everyone again
 
Upvote 0
spoke too soon.

when I copy and run the codes in a module, nothing happened!
have I missed something?
 
Upvote 0
Is your layout like mine with the pasted values from the website in column C like I have?
Was that sheet the active sheet when the code was run?
Do you have columns E:I visible?
How did you run the code?
 
Upvote 0
never mind, just spotted an error.
i started the values in Col D instead of C, it's all working now

cheers
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top