Copying An Array To A New Array Gives subscript out of range error

Roonyroox

New Member
Joined
Aug 25, 2020
Messages
22
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi, Im trying to copy a value from an array to a new array, ie., Array1(2, 1) = Array2(1, 1)

But this gives a subscript out of range error, the code is pretty basic.

Link to the file

VBA Code:
Sub x1()
 
     
    Dim Array1() As Variant
    Dim Array2() As Variant
    
    Array1 = Range("a1:a10").Value
    
    
    
    Array1(2, 1) = Array2(1, 1)



End Sub
 
Rory, im trying to figure out how to use arrays, also this is code for a larger table.
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
This is just test code to try & learn how to use arrays, i couldnt figure out a few things on arrays using google, so i decided to ask for some help here
 
Upvote 0
It should be:

Code:
Sub x1()
Dim Array1 As Variant
Dim Array2() As Variant


Array1 = Range("a1:c10").Value
ReDim Array2(1 To UBound(Array1), 1 to Ubound(array1, 2))

     Array2(1, 3) = Array1(1, 3)

Range("e1") = Array2(1, 3)

End Sub
 
Upvote 0
Awesome, thanks guys, saved me alot of time trying to figure out the basics of arrays. Much appreciated.

Thanks.
 
Upvote 0
Oh btw, why doesnt Dim Array1 have brackets, but Array2 has () brackets?
 
Upvote 0
You can use brackets if you like. I tend to just declare as Variant, rather than as an array.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,739
Members
449,050
Latest member
excelknuckles

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