Array not returning expected value

ToExcelOrNotToExcel

New Member
Joined
Jan 7, 2023
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone, I'm trying to loop through a range of cells and have and store them in a 2d array. I'll post my data below but firstly I'm I want it to separate everything by ";" and then for the second dimension by ",". I want to perform some calculations but they work this code doesn't. I don't know if i should destroy the array after every loop or how to do that. anyway here is the data below.

Betting forula 2022.xlsm
CE
3HM9;AM17;HM30;AM31;AM50;HM60;AM75;AM86;HM96;HM104;AM110;HM126
4AM31,10;AM50,31;HM60,10;AM75,31;AM86;HM96;HM104,87;AM110;HM126
5HA60,58;AA75;AA86;HA96;HA104,76;AA110
6HA9;AA17;HA30,86;AA31;AA50,87;HA60,80;AA75,59;AA86;HA96,18;HA104;AA110,81;HA126
7HA9,37;AA17;HA30,4;AA31,22;AA50,22;HA60,32;AA86,61;HA96,72;HA126
8HM9;AM17,17;HM30,86;AM31;AM50;HM60;AM75;AM86,83;HM104,3;AM110,80;HM126,79
9HD9;AD17,46;AD75,44;AD86;HD96;HD104;AD110;HD126
10HD9;AD17,40;HD30;AD31;AD50;HD80,80;AD75;AD86;HD96;HD104;AD110;HD126
11HD9;AD17;HD30,86;AD31;AD50;HD60;AD75;AD86;HD96;HD104;AD110;HD126
12HA9;AA17,60;HA30,71;AA31,68;AA50,68;HA60,67;AA75,70;HA96;HA104,87;AA110,52
13HM9,78;AM17,44;HM30;AM31;AM50;HM60;AM75,59;AM86,7;HM104,14;AM110,10;HM126,29
14AD17,44;HD30;AD31;AD50;HD60;AD75,46
15AD17,44;HD30;AD31;AD50;HD60;AD75,40;HD96;HD104;AD110,60
16AD75,50;AD86;AD110,30
17HD9;AD17;AD60,9;HD126
18HGK9;AGK17;HGK30;AGK31;AGK50;HGK60;AGK75;AGK86;HGK96;HGK104;AGK110;HGK126
19HA30,44;AA75,31;AA86,29
20HD30,4
21HM9,53;AM17,46;HM30,19;AM50,3;HM60,23;AM75,20;HM96;HM104;AM110,38;HM126,11
22AA17,30;HA30,46;AA31,80;AA50,59;HA104,3;AA110,9;HA126,61
23
24HM9,12;AM17,3;HM30,4
Football


Here is the code below. I will use the last cell CE24 as an example of what I want. arr_2d(0,0) = HM9, arr_2d(0,1) = 2, arr_2d(1,0) = AM17, arr_2d(1,1) = 3

Code:
Option Explicit
Sub FillPlayerRating()

    Dim rng As Range, cell As Range
    Dim ArrayCount As Integer
    Dim i As Long
    Set rng = Range("CE3:CE24")
    Dim size_1d As Integer
    
        For Each cell In rng
        
            If Len(cell.Value) > 0 Then
                
                Dim arr_1d() As String
                arr_1d = Split(cell.Value, ";")
                size_1d = UBound(arr_1d) - LBound(arr_1d)
                
                Range("CS20") = size_1d
                
                Dim arr_2d() As String
                ReDim arr_2d(size_1d, 1) As String
        
                    For i = 0 To size_1d
                    Dim temp() As String
                    temp = Split(arr_1d(i), ",")
                    Dim size_temp As Integer
                    size_temp = UBound(temp) - LBound(temp) + 1
                    If size_temp = 1 Then
                        arr_2d(i, 0) = Trim(temp(0))
                    ElseIf size_temp = 2 Then
                        arr_2d(i, 0) = Trim(temp(0))
                        arr_2d(i, 0) = Trim(temp(1))
                    End If
                      
                    Next i
                            
            End If
            
        Next cell

End Sub

[\code]
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
HM9,12;AM17,3;HM30,4
You mean:
I will use the last cell CE24 as an example of what I want. arr_2d(0,0) = HM9, arr_2d(0,1) = 12, arr_2d(1,0) = AM17, arr_2d(1,1) = 3

I'm not sure what you're going to do with the data in the array, but with the following code, the second array contains one row for each ";" separator, and in that row in column 1 the first data and in column 2 the second data if it has a comma as a separator.
The result of puts it in the CG and CH columns.

VBA Code:
Sub FillPlayerRating()
  Dim cell As Range
  Dim i As Long, n As Long
  Dim p As Variant, arr_2d As Variant
  
  With Range("CE3:CE24")
    n = Evaluate("=SUM(LEN(" & .Address & ")-LEN(SUBSTITUTE(" & .Address & ","";"",""""))+1)")
    ReDim arr_2d(1 To n, 1 To 2)
  
    For Each cell In .Cells
      For Each p In Split(cell, ";")
        i = i + 1
        arr_2d(i, 1) = Trim(Split(p, ",")(0))
        If InStr(1, p, ",") > 0 Then arr_2d(i, 2) = Trim(Split(p, ",")(1))
      Next
    Next
  End With
  Range("CG3").Resize(i, 2).Value = arr_2d
End Sub

Example:
Dante Amor
CECFCGCH
1
2
3HM9;AM17;HM30;AM31;AM50;HM60;AM75;AM86;HM96;HM104;AM110;HM126HM9
4AM31,10;AM50,31;HM60,10;AM75,31;AM86;HM96;HM104,87;AM110;HM126AM17
5HA60,58;AA75;AA86;HA96;HA104,76;AA110HM30
6HA9;AA17;HA30,86;AA31;AA50,87;HA60,80;AA75,59;AA86;HA96,18;HA104;AA110,81;HA126AM31
7HA9,37;AA17;HA30,4;AA31,22;AA50,22;HA60,32;AA86,61;HA96,72;HA126AM50
8HM9;AM17,17;HM30,86;AM31;AM50;HM60;AM75;AM86,83;HM104,3;AM110,80;HM126,79HM60
9HD9;AD17,46;AD75,44;AD86;HD96;HD104;AD110;HD126AM75
10HD9;AD17,40;HD30;AD31;AD50;HD80,80;AD75;AD86;HD96;HD104;AD110;HD126AM86
11HD9;AD17;HD30,86;AD31;AD50;HD60;AD75;AD86;HD96;HD104;AD110;HD126HM96
12HA9;AA17,60;HA30,71;AA31,68;AA50,68;HA60,67;AA75,70;HA96;HA104,87;AA110,52HM104
13HM9,78;AM17,44;HM30;AM31;AM50;HM60;AM75,59;AM86,7;HM104,14;AM110,10;HM126,29AM110
14AD17,44;HD30;AD31;AD50;HD60;AD75,46HM126
15AD17,44;HD30;AD31;AD50;HD60;AD75,40;HD96;HD104;AD110,60AM3110
16AD75,50;AD86;AD110,30AM5031
17HD9;AD17;AD60,9;HD126HM6010
18HGK9;AGK17;HGK30;AGK31;AGK50;HGK60;AGK75;AGK86;HGK96;HGK104;AGK110;HGK126AM7531
19HA30,44;AA75,31;AA86,29AM86
20HD30,4HM96
21HM9,53;AM17,46;HM30,19;AM50,3;HM60,23;AM75,20;HM96;HM104;AM110,38;HM126,11HM10487
22AA17,30;HA30,46;AA31,80;AA50,59;HA104,3;AA110,9;HA126,61AM110
23HM126
24HM9,12;AM17,3;HM30,4HA6058
25AA75
26AA86
27HA96
28HA10476
29AA110
30HA9
31AA17
32HA3086
33AA31
34AA5087
35HA6080
36AA7559
37AA86
38HA9618
39HA104
40AA11081
41HA126
42HA937
43AA17
44HA304
45AA3122
46AA5022
47HA6032
48AA8661
49HA9672
50HA126
51HM9
52AM1717
53HM3086
54AM31
55AM50
56HM60
57AM75
58AM8683
59HM1043
60AM11080
61HM12679
62HD9
63AD1746
64AD7544
65AD86
66HD96
67HD104
68AD110
69HD126
70HD9
71AD1740
72HD30
73AD31
74AD50
75HD8080
76AD75
77AD86
78HD96
79HD104
80AD110
81HD126
82HD9
83AD17
84HD3086
85AD31
86AD50
87HD60
88AD75
89AD86
90HD96
91HD104
92AD110
93HD126
94HA9
95AA1760
96HA3071
97AA3168
98AA5068
99HA6067
100AA7570
101HA96
102HA10487
103AA11052
104HM978
105AM1744
106HM30
107AM31
108AM50
109HM60
110AM7559
111AM867
112HM10414
113AM11010
114HM12629
115AD1744
116HD30
117AD31
118AD50
119HD60
120AD7546
121AD1744
122HD30
123AD31
124AD50
125HD60
126AD7540
127HD96
128HD104
129AD11060
130AD7550
131AD86
132AD11030
133HD9
134AD17
135AD609
136HD126
137HGK9
138AGK17
139HGK30
140AGK31
141AGK50
142HGK60
143AGK75
144AGK86
145HGK96
146HGK104
147AGK110
148HGK126
149HA3044
150AA7531
151AA8629
152HD304
153HM953
154AM1746
155HM3019
156AM503
157HM6023
158AM7520
159HM96
160HM104
161AM11038
162HM12611
163AA1730
164HA3046
165AA3180
166AA5059
167HA1043
168AA1109
169HA12661
170HM912
171AM173
172HM304
Hoja6
 
Upvote 0
The following code does the same thing as the previous one.

VBA Code:
Sub test3()
  Dim c
  c = Split(Join(Application.Transpose(Range("CE3:CE24").Value), ";"), ";")
  With Range("CG3").Resize(UBound(c) + 1)
    .Value = Application.Transpose(c)
    .TextToColumns .Cells(1), xlDelimited, Comma:=True
  End With
End Sub
 
Upvote 1
Solution
The following code does the same thing as the previous one.

VBA Code:
Sub test3()
  Dim c
  c = Split(Join(Application.Transpose(Range("CE3:CE24").Value), ";"), ";")
  With Range("CG3").Resize(UBound(c) + 1)
    .Value = Application.Transpose(c)
    .TextToColumns .Cells(1), xlDelimited, Comma:=True
  End With
End Sub
hi, you used the ";" twice shouldn't the second one be a comma "," and is c is a 2d array?
 
Upvote 0
You mean:


I'm not sure what you're going to do with the data in the array, but with the following code, the second array contains one row for each ";" separator, and in that row in column 1 the first data and in column 2 the second data if it has a comma as a separator.
The result of puts it in the CG and CH columns.

VBA Code:
Sub FillPlayerRating()
  Dim cell As Range
  Dim i As Long, n As Long
  Dim p As Variant, arr_2d As Variant
 
  With Range("CE3:CE24")
    n = Evaluate("=SUM(LEN(" & .Address & ")-LEN(SUBSTITUTE(" & .Address & ","";"",""""))+1)")
    ReDim arr_2d(1 To n, 1 To 2)
 
    For Each cell In .Cells
      For Each p In Split(cell, ";")
        i = i + 1
        arr_2d(i, 1) = Trim(Split(p, ",")(0))
        If InStr(1, p, ",") > 0 Then arr_2d(i, 2) = Trim(Split(p, ",")(1))
      Next
    Next
  End With
  Range("CG3").Resize(i, 2).Value = arr_2d
End Sub

Example:
Dante Amor
CECFCGCH
1
2
3HM9;AM17;HM30;AM31;AM50;HM60;AM75;AM86;HM96;HM104;AM110;HM126HM9
4AM31,10;AM50,31;HM60,10;AM75,31;AM86;HM96;HM104,87;AM110;HM126AM17
5HA60,58;AA75;AA86;HA96;HA104,76;AA110HM30
6HA9;AA17;HA30,86;AA31;AA50,87;HA60,80;AA75,59;AA86;HA96,18;HA104;AA110,81;HA126AM31
7HA9,37;AA17;HA30,4;AA31,22;AA50,22;HA60,32;AA86,61;HA96,72;HA126AM50
8HM9;AM17,17;HM30,86;AM31;AM50;HM60;AM75;AM86,83;HM104,3;AM110,80;HM126,79HM60
9HD9;AD17,46;AD75,44;AD86;HD96;HD104;AD110;HD126AM75
10HD9;AD17,40;HD30;AD31;AD50;HD80,80;AD75;AD86;HD96;HD104;AD110;HD126AM86
11HD9;AD17;HD30,86;AD31;AD50;HD60;AD75;AD86;HD96;HD104;AD110;HD126HM96
12HA9;AA17,60;HA30,71;AA31,68;AA50,68;HA60,67;AA75,70;HA96;HA104,87;AA110,52HM104
13HM9,78;AM17,44;HM30;AM31;AM50;HM60;AM75,59;AM86,7;HM104,14;AM110,10;HM126,29AM110
14AD17,44;HD30;AD31;AD50;HD60;AD75,46HM126
15AD17,44;HD30;AD31;AD50;HD60;AD75,40;HD96;HD104;AD110,60AM3110
16AD75,50;AD86;AD110,30AM5031
17HD9;AD17;AD60,9;HD126HM6010
18HGK9;AGK17;HGK30;AGK31;AGK50;HGK60;AGK75;AGK86;HGK96;HGK104;AGK110;HGK126AM7531
19HA30,44;AA75,31;AA86,29AM86
20HD30,4HM96
21HM9,53;AM17,46;HM30,19;AM50,3;HM60,23;AM75,20;HM96;HM104;AM110,38;HM126,11HM10487
22AA17,30;HA30,46;AA31,80;AA50,59;HA104,3;AA110,9;HA126,61AM110
23HM126
24HM9,12;AM17,3;HM30,4HA6058
25AA75
26AA86
27HA96
28HA10476
29AA110
30HA9
31AA17
32HA3086
33AA31
34AA5087
35HA6080
36AA7559
37AA86
38HA9618
39HA104
40AA11081
41HA126
42HA937
43AA17
44HA304
45AA3122
46AA5022
47HA6032
48AA8661
49HA9672
50HA126
51HM9
52AM1717
53HM3086
54AM31
55AM50
56HM60
57AM75
58AM8683
59HM1043
60AM11080
61HM12679
62HD9
63AD1746
64AD7544
65AD86
66HD96
67HD104
68AD110
69HD126
70HD9
71AD1740
72HD30
73AD31
74AD50
75HD8080
76AD75
77AD86
78HD96
79HD104
80AD110
81HD126
82HD9
83AD17
84HD3086
85AD31
86AD50
87HD60
88AD75
89AD86
90HD96
91HD104
92AD110
93HD126
94HA9
95AA1760
96HA3071
97AA3168
98AA5068
99HA6067
100AA7570
101HA96
102HA10487
103AA11052
104HM978
105AM1744
106HM30
107AM31
108AM50
109HM60
110AM7559
111AM867
112HM10414
113AM11010
114HM12629
115AD1744
116HD30
117AD31
118AD50
119HD60
120AD7546
121AD1744
122HD30
123AD31
124AD50
125HD60
126AD7540
127HD96
128HD104
129AD11060
130AD7550
131AD86
132AD11030
133HD9
134AD17
135AD609
136HD126
137HGK9
138AGK17
139HGK30
140AGK31
141AGK50
142HGK60
143AGK75
144AGK86
145HGK96
146HGK104
147AGK110
148HGK126
149HA3044
150AA7531
151AA8629
152HD304
153HM953
154AM1746
155HM3019
156AM503
157HM6023
158AM7520
159HM96
160HM104
161AM11038
162HM12611
163AA1730
164HA3046
165AA3180
166AA5059
167HA1043
168AA1109
169HA12661
170HM912
171AM173
172HM304
Hoja6
hi what does "CG3" have to do with anything
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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