3

in the following piece of code, VBA tells me that boundary has to be a constant on the Dim vals(1 To boundary) As Variant line. This is the first time I see this. What's happening here ?

Dim boundary As Integer
boundary = distrib.Count

Dim vals(1 To boundary) As Variant
For i = 1 To boundary
    vals(i) = distrib(i, 3) + distrib(i, 4)
Next i

distrib is an array declared as variant

3 Answers 3

4

You cannot declare a fixed size array with a non-constant value; instead you need to declare it as a dynamic array (by omitting a size) then redimension it to your desired bounds;

Dim vals() As Variant
ReDim vals(1 To boundary)
Sign up to request clarification or add additional context in comments.

Comments

2

You can't assign data in the same operation as declaring a variable. Try a Redim instead

Dim boundary As Long
boundary = 10
Dim vals() As Variant
ReDim vals(1 To boundary)

Comments

2

You can'd declare an array initially with variable bounds, however you can ReDim then to variables:

Dim boundary As Integer
boundary = distrib.Count

Dim vals() as Variant
ReDim vals(1 To boundary)
For i = 1 To boundary
    vals(i) = distrib(i, 3) + distrib(i, 4)
Next i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.