Question - Constant to Array function

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,596
Office Version
  1. 2016
Platform
  1. Windows
Hi dear forum members.

Consider the following:
Code:
Const MyConst = "A" & "B" & "C" & "D"

Sub Test()
    Dim Myarray() As Variant
    
    Myarray = Array(MyConst)
    Debug.Print Myarray(0); Myarray(1); Myarray(2); Myarray(3)
End Sub

Of course, the above code won't work but you get the idea ... What I am looking for is to be able to populate the array MyArray with the 4 letters A,B,C,D based on the Const MyConst just like you woud using the following:
Code:
Myarray = Array("A", "B", "C", "D")

I have tried a few string manipulations/Evaluate etc ... but with no sucess... Does anybody have an idea how/if this could be done ?

Thank you.
 
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Use Split?

Code:
Const MyConst = "A,B,C,D"
myArray = Split(MyConst, ",")
 
Upvote 0
Use Split?

Code:
Const MyConst = "A,B,C,D"
myArray = Split(MyConst, ",")

That was nice and simple - How could I have missed that ! lol
I spent an hour trying all kinds of string manimpulations but never thought of using Split.
Thanks RoryA.
 
Upvote 0
Glad to help. It was probably the lack of API calls that threw you off... :biggrin:
 
Upvote 0
You could also do it like this.
Code:
Const MyConst = "ABCD"
Dim MyConstUni As Variant
Dim MyArray As Variant

    MyConstUni = StrConv(MyConst, vbUnicode)
    
    MyArray = Split(Left(MyConstUni, Len(MyConstUni) - 1), Chr(0))
 
Upvote 0
You could also do it like this.
Code:
Const MyConst = "ABCD"
Dim MyConstUni As Variant
Dim MyArray As Variant

    MyConstUni = StrConv(MyConst, vbUnicode)
    
    MyArray = Split(Left(MyConstUni, Len(MyConstUni) - 1), Chr(0))

That also worked ... Thank you Norie.
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,406
Members
448,958
Latest member
Hat4Life

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