1

I've found something that I don't quite understand. Please help explain.

so I used this code to assign values within named ranges to an array. (see below).

Dim accountStructure As Variant
ReDim accountStructure(2)
accountStructure(0) = Range("namedrange1")
accountStructure(1) = Range("namedrange2")
accountStructure(2) = Range("namedrange3")

The code works. However, in order to call the first item in accountstructure(0) I need to type accountStructure(0)(1,1), second item accountStructure(0)(2,1) third item (0)(3,1) and so on. I understand why there is (0) there but I don't understand why there is always a 1 after the item number. Please help me understand this.

4
  • 1
    You can (and should for performance reasons) manipulate ranges assigning them to arrays. So, if we have any range Dim rng as Range Set rng=Range("A1:A10") We can assign Dim arr As varient arr=rng But we have to treat it as two dimensional array. Because Excel VBA does'nt "know" it is one dimensional. So we can now For i=1 To 10 arr(i,1) = i Next Commented Jan 31, 2018 at 12:41
  • thanks TSion, so it will always be 2 dimensional, just how VBA works? Unfortunately the namedranges are dynamic (in a table). so this way is super easy. :) Commented Jan 31, 2018 at 12:44
  • Excel is a 2 dimensional planet ;-) Commented Jan 31, 2018 at 12:45
  • Thanks for your clarification :) Commented Jan 31, 2018 at 12:47

1 Answer 1

1

If we have any range

Dim rng as Range 
Set rng=Range("A1:A10") 

We can assign

Dim arr As Variant 
arr = rng 

But we have to treat it as two dimensional array. Because Excel VBA does'nt "know" it is one dimensional.
So we can now

For i=1 To 10 
    arr(i,1) = i 
Next
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.