tags:

views:

165

answers:

3

We generally use the array function is VBA as :

Dim A As Variant

A = Array("B", 1)

This will give me the first element in A as "B" and second element as 1

However I want to decide the contents of A at run-time so is it 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 first element in A as "name, Sam", but I need first element as "name" and second element as "Sam".

What could be the solution to this? How could I populate A at run-time?

+1  A: 

It appears that you are looking for a Dictionary object or an associative array structure. An example of one can be found here:

http://stackoverflow.com/questions/915317/does-vba-have-dictionary-structure

Robert Harvey
I have read about the dictionary object, but I was looking for some solutions using an array because I have some legacy code that I have to cater to and I don't want to make major changes in it... the legacy code uses an Array as one of its parameters which I have to feed in.
Kevin Boyd
+7  A: 

You can just use VBA's Split function.

Dim A as Variant
A = Split(str, ",")
Reed Copsey
Though for the given example the output of `Split()` either needs some trimming or the string is prepared with some regex like `\s*,\s*`.
Tomalak
Good one... but if the string consists of str = "name, Sam, age, 24" can the 24 be loaded into the array as an Integer because its now being loaded as a string?
Kevin Boyd
VBA should let you just assign the string ("24") to an integer, though, since it's not very strict about those things...
Reed Copsey
Looks like you need a small function to do that. Make an Array-of-Variant returning function that accepts the string as an argument, calls `Split()` on it, and ReDims the output array to proper size based in `UBound()`. Then fill the array one by one with the help of `IsNumeric()` and `CInt()`.
Tomalak
@Reed Copsey: But as a result of Split(), you will get a string, and it will stay a string. VBA is strongly typed, only the Variant type gives the impression of duck-typing. (VBScript is different, there you have Variants only, which essentially makes the whole language look like it is duck-typed. Which it still isn't, strictly speaking.)
Tomalak
@Tomalak: With option strict off, you can assign a variable in a string to an integer in VBA, I thought...
Reed Copsey
@Tomalak, Reed. You can always assign a string to an integer in VBA. It's called evil type coercion. VB.Net introduced Option Strict so it can be avoided, but Option Strict doesn't exist in VBA. (Why do you say this isn't duck typing? It looks like duck typing and it quacks like duck typing.)
MarkJ
@MarkJ: Yeah, I know you could do this. I thought strict typing was added in more recent versions of VBA, though - but I'm not quite as familiar with it (last time I used it heavily was in office 2000...) Thanks for the update.
Reed Copsey
A: 
A = Array(split(str,","))

but for your purposes you should go with Robert Harvey's link.

avguchenko
This returns a 2D array with A(0)(0) = "name", A(0)(1) = "Sam"
Kevin Boyd
yes. imagine a table where the column name is in the first column, the value in the second. then A(x)(0) returns variable name, A(x)(1) returns its value.
avguchenko
@Kevin: The `Split` function already returns an array, so `Array(Split(str, ","))` creates an single-element array containing an array.
Mike Spross