Array as Range

smpatty08

Board Regular
Joined
May 16, 2014
Messages
155
I am trying to learn more about arrays and I am trying to set an array to a range of cells (B5:M12). I am getting an error in the following code.
Code:
Sub Macro3()Dim myArray() As Range, x, y As Integer


For x = 2 To 13
    For y = 5 To 12
        Set myArray(x, y) = Range(Cells(y, x))
    Next y
Next x
End Sub

Can somebody help me understand this? Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Easiest way is
Code:
Sub Macro3()
   Dim Ary As Variant
   Ary = Range("B5:M12").value2
End Sub
 
Upvote 0
Easiest way is
Code:
Sub Macro3()
   Dim Ary As Variant
   Ary = Range("B5:M12").value2
End Sub

Thanks! But what if I would like to move the information from ary(1,1 to 12) to ary(2, 1 to 12)? I think I could simplify a lot of code I use if this is possible.
 
Upvote 0
You can do that like
Code:
Sub Macro3()
   Dim Ary As Variant
   Dim i As Long
   Ary = Range("B5:M12").value2
   For i = 1 To UBound(Ary, 2)
      Ary(2, i) = Ary(1, i)
   Next i
End Sub
But you will lose whatever was originally in Ary(2,x)
 
Upvote 0

Forum statistics

Threads
1,215,759
Messages
6,126,730
Members
449,333
Latest member
Adiadidas

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top