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
 
You've only populated one value in array2, and only sent the output to one cell, so you should see one value (assuming A1 wasn't blank)
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Why pass to Second Array and then to Range...Why not just directly to Range from First Array
 
Upvote 0
As per Post 9...
VBA Code:
Sub sintek()
Dim Arr1, Arr2
Arr1 = Range("A1:A10").Value
ReDim Arr2(1 To 1)
Arr2(1) = Arr1(2, 1)
Range("E1") = Arr2(1)
End Sub

In your case...?

VBA Code:
Range("e1:e1") = Array2(1, 1)
 
Upvote 0
Thanks sintek & rory, works fantastically, much appreciated ...
 
Upvote 0
Hi guys one last question.,

Is it possible to make Array2 dynamic, so it autoadjusts the size just like Array1? ie Dim Array2() As Variant. I'm using atm Dim Array2(1, 10) As Variant.

Thanks.,


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


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


End Sub
 
Upvote 0
Why are you using an array at all? You only populate one element.
 
Upvote 0
Sintek thanks, is this the correct way to use it?

VBA Code:
Sub x1()
 
     
     
     
     
     
     
    Dim Array1() As Variant
    Dim Array2(1, 10) As Variant
    
  
    Array1 = Range("a1:c10").Value
    ReDim Array2(1 To UBound(Array1))
    
    
     Array2(1, 3) = Array1(1, 3)


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


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

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