1

I´d like to add a row to a variant array:

Dim arrMod As Variant
arrMod(numberOfRow) = Array(myValue1, myValue2, myvalue3)

The execution of this code results into an exception: Error 13: type mismatch How can I do it without iterating each column?

Thanks,

Regards

2 Answers 2

2

Your variable arrMod is not an array. You need to define it in VBA as an array using parenthesis:

Dim arrMod(0) As Variant

Obviously replace 0 with the maximum number of rows you have, or resize dynamically using redim preserve.

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

2 Comments

This is a good solution, but I don´t know the array size (it would depend of some values read in each iteration). How can I prevent redim in each iteration?
If you are unable to correctly size the array at instantiation then you don't really have much choice but to redim on each iteration as the VBA array is mutable. You could look at immutable collection types?
1

Do you need something like this:

Dim arrMod()
For i = 1 To 5
    ReDim Preserve arrMod(i)
    arrMod(i) = i
    MsgBox Join(arrMod, vbCrLf)
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.