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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Array with variable size 1

Status
Not open for further replies.

jhogie

Technical User
Jun 3, 2008
24
CA
This seems simple, but I can't figure it out, and I couldn't find in another forum. Below is my simplified code. I want to create an array ('Tag') that will contain string variables. The array should be the size of 'TagCount' (ie. 10).

I get the error "Constant Expression Required"
Code:
Sub test()
Dim TagCount As Integer
TagCount = 10
Dim Tag(TagCount) As String
End Sub
Any help would be much appreciated. Thanks for taking a look,

- JHogie
 
You need to dimension the variable without a size and then Redim it to the size you want.

Code:
Sub test()

  Dim TagCount As Integer
  TagCount = 10

  Dim Tag() As String
  Redim Tag(TagCount-1)

End Sub

By default, arrays start at zero, so if you want 10 elements, you need to redim the array to 9.

Make sense?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
>so if you want 10 elements, you need to redim the array to 9

or use a non zero based array;

Redim Tag(1 To TagCount)
 
I did know that arrays start at zero. But I really like the trick for redimensioning the variable... it did the trick, THANKS
 
But if you know the size of the array why not just declare it as Dim Tag(9) As String


[gray]Experience is something you don't get until just after you need it.[/gray]
 
I simply posted a simplified version of my module. In my ACTUAL program it is not 10 (or 9), it IS a variable.

It is a legitamate question. Thanks for posting.
 

For further info

Option Base 1

Press F1-Index tab-Option Base statement

or of undetermined number of elements use LBound and UBound
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top