Alternative use of Array () function in VBA?
We usually use VBA array function like:
Dim A As Variant
A = Array("B", 1)
This will give me the first item in as "B" and the second item as 1
However, I want to resolve the content of A at runtime, so that it is possible for me to do something like
Dim str As String
Dim A As Variant
str = "name, Sam"
A = Array(str)
When I run this code, it gives me the first element in A
as "name, Sam"
, but I need the first element as "name"
and the second as "Sam"
.
What could be the solution to this? How can I fill A
in at runtime?
You can just use VBA's split feature .
Dim A as Variant
A = Split(str, ",")
You seem to be looking for a Dictionary object or an associative array structure. An example can be found here:
Does VBA have a dictionary structure?
A = Array(split(str,","))
but for your purposes you should go with the Robert Harvey link.
In fact, A = Split (str, ",") works fine and returns a one-dimensional array.