Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Problem

Status
Not open for further replies.

dace

Programmer
Jul 21, 2001
263
US
Hi-

I have a string array of variable size (I redimension it on the fly). I need to be able to get a variant that is an array of the contents of the string array-

for example, say I had stringArray(0) to stringArray(2)

I need to be able to declare something like:

dim differentKindOfArray as Variant

differentKindOfArray = Array(stringArray(0), stringArray(1), stringArray(2))


I have to do it this way because the string arrays are parsed pieces of an SQL query I'm passing to an Excel pivottable. My problem is, I don't know how many elements there will be in the StringArray(), so I can't explicitly declare it like above, I need a way to do it dynamically.

Any help would be greatly appreciated!
Thank you!
 

You can always use the UBound function to find the number of elements...
[tt]
NumberOfElements = UBound(ArrayName)
[/tt]
So in your case it would be...
[tt]
NumberOfElements = Ubound(stringArray)
[/tt]

Good Luck

 
Hi,

You want Redim Preserve MyArray(NewIndex). Check out help

:)

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Sorry, perhaps I hadn't been as clear as I should have been-

I have been redimensioning my string array every time I add an element as follows:

ReDim Preserve MyArray(0 To (UBound(MyArray) + 1)) As String

After this, however, I do the following:

dim DifferentArray As Variant
Different Array = Array(MyArray(0), MyArray(1), MyArray(2))

BUT my problem is that I don't know how many MyArray(x) there will be. I know I can get the number of elements in it, but I only know how to declare a a variant array like that above explicitly. I need to be able to declare it with a different number of elements from MyArray() dynamically in-code.
 
Okay, I managed to solve my problem by redimensioning my string array and casting everything to a variant- thanks for the help though!
 
Code:
dim MyArray(), idx as integer
idx=0
Do
  redim Preserve MyArray(Idx)
  'now load the new element

  idx = idx + 1
Loop
[code]


Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top