VBA- How to split text to second dimension of an array

  • Thread starter Thread starter Legacy 143009
  • Start date Start date
L

Legacy 143009

Guest
I wonder if is there a syntax for spilling out a split function among the second dimension of an array:

VBA Code:
Redim myArr(i, 1)
myArr(i, 0) = Split("A,B", ",")

I expect the text will be spilled as myArr(i, 0)=A and myArr(i, 1)=B since the dimension lengths was predefined.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Question is not at all clear to me. Can you give us more of the code and more detailed explanation of what you have and what you are trying to achieve?
 
Upvote 0
Thanks @Peter_SSs

Infact that's all. To be more precise when you input like [myArr = Split("A,B", ",")] normally it spreads A and B as [myArr(0) = A] and [myArr(1) = B] automatically.

What I am asking is, if I keep the first dimension fixed, let say "n", how can I split the string across second dimension as [myArr(n, 0)=A] and [myArr(n, 1)=B] automatically?

I hope it is clear..
 
Upvote 0
I want to create couples. I have a code that keeps couples in a one-dimensional array. Maybe I have to fix that code but I don't want to go back.
Currently my one-dimensional array looks like this:
VBA Code:
namesArr(0) = "A,B"
namesArr(1) = "C,D"
namesArr(2) = "E,F"

I am seeking for a short cut to create:
VBA Code:
couplesArr(0, 0) = "A"
couplesArr(0, 1) = "B"
couplesArr(1, 0) = "C"
couplesArr(1, 1) = "D"
couplesArr(2, 0) = "E"
couplesArr(2, 1) = "F"

I am asking only for a short hand. If there is no such short hand syntax, I can split them one by one. No problem.
 
Upvote 0
For that example you could use
VBA Code:
   Dim couplesArr As Variant
   couplesArr = [{"A","B";"C","D";"E","F"}]
Note. This will be a 1 based array
 
Upvote 0
Thanks @Fluff
As you know when you have have code like this it is ok to split text directly to an array:
VBA Code:
Sub test()
  Dim names() As String

  names = Split("A,B;C,D;E,F", ";") 'You can directly index 3 items into 1 based array at once.
  'At this point, you don't have to do anything extra like pointing an index number.

  For i = 0 to 2
    Debug.Print names(i)
  Next
End Sub
'Debug console will print the following without a problem:
'A,B
'C,D
'E,F
But you can't apply the same method in line 4 above to a 2 based array. In this example code will give an error:
VBA Code:
Sub test()
  'Resume to previous code
  Dim couples() As String

  For i = 0 to 2
    couples(i) = Split(names(i), ";") 'I am asking for this line. The code will throw an error at this point
    'At this point what would I expect is A and B values could be directly indexed as couples(i, 0) and couples(i, 1), let's say, "automatically".
    'But it will need index number for the second base.
  Next
End Sub
That I expect a result as below. Maybe I am setting the wrong model in my mind, I don't know.
1667982188853.png

Briefly what I am asking for is, is there a shorthand way to split a string to the second base of a 2-base array?
If it is not clear still, never mind. I was just curious about it...
 
Upvote 0
Both those codes are using 1D arrays, so will not create a 2D array.
If you are trying to create a 2D array form a 1D array, then you will need to loop through both dimensions.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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