VBA assign a range to an Array from different shee

2019-08-05 21:01发布

问题:

I have two different sheets and i have to assign 2 ranges from these two sheets to two different array but the problem is i can not specify a sheet before range for e.g

Dim flArr() as variant 
flArr = Sheets("xxx").range(A1:A10)

This gives me an error. any workaround?

Thanks

回答1:

Look at the differences between your code and mine. Remove the parenthesis after your variant variable, let excel figure out it is an array. Also you need to include the range in quotations and specify that you want the values from the range.

Dim flArr As Variant
flArr = Sheets("xxx").Range("A1:A10").Value

Hope this helps :)

You could also do some less common construction as this, but it will be just awkward.

Dim arr() As Variant
ReDim arr(1 To 10)
arr() = Sheets("xxx").Range("A1:A10").Value

Hope this helps



标签: arrays vba range