howdybaby
howdybaby

Reputation: 307

Setting array length as variable value?

I was wondering if it's possible to set an array's length as a variable value as it would help a lot with my current function.

Essentially what I want to be able to do is:

q = 2
Dim AnswerIDs(q)

However, this results in an error, does anyone know how I could do this?

Upvotes: 1

Views: 987

Answers (2)

Wasim
Wasim

Reputation: 1174

You can do as follows:

q = 2
Dim AnswerIDs(), i
ReDim AnswerIDs(-1)
i = 0
Do While q > i
  ReDim Preserve AnswerIDs(UBound(AnswerIDs) + 1)
  AnswerIDs(UBound(AnswerIDs)) = "test"
  i = i + 1
Loop

Upvotes: 0

stovroz
stovroz

Reputation: 7075

q = 2
Dim AnswerIDs()
Redim AnswerIDs(q)

Upvotes: 4

Related Questions