0

This is my code:

Function test(data As Range)

Const nobs As Integer = data.Columns.Count

'Const nobs As Integer = 2  <- this one works, but not dynamic

Dim AMatrix(1 To nobs, 1 To nobs) As Variant

End Function

Dim needs a constant value in it's argument. How can I use my dynamically changing nobs variable instead?

1 Answer 1

3

You must use the ReDim statement and your Nobs must be a variable not a constant.

Dim Nobs As Long
Nobs = Data.Columns.Count

ReDim AMatrix(1 To Nobs, 1 To Nobs) As Variant

Note that Columns.Count returns a Long not an Integer.

Also note that your Function should either return a value or you should switch it to a procudure Sub.

Sign up to request clarification or add additional context in comments.

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.