Copy one array to an other

chuckchuckit

Well-known Member
Joined
Sep 18, 2010
Messages
541
Can I copy all of the elements from one array to the other easily without looping through each element row and column? I would then be deleting all elements from one of the arrays.
Code:
Dim MyArray(200, 30)
Dim MyArray_BKUP(200, 30)
I was trying various forms of "With" type coding, using Resize etc that works with "Cells" and "Ranges", but can't seem to get anything similar to work with arrays.

Thanks.
Chuck
 
No, the array is completely independent of any cell.
In my example it's only meant to design an array very fast and populating it at the same time.
Just change values in array sq and you will see.

So for the non-believers:

Code:
Sub stst()
  ReDim sq(200, 30)
  sq(10, 10) = "example"
  sn = sq
 
  Sheets(1).Cells(5, 1).Resize(UBound(sn), UBound(sn, 2)) = sn
End Sub
 
Last edited:
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Code:
Dim MyArray(200, 30)
Dim MyArray_BKUP(200, 30)

Hi Chuck,

What I meant was that it appeared that you wanted to transpose the MyArray into MyArray_BKUP, as your example arrays are sized in such a manner. That is, MyArray is 21 rows x 31 columns, where the backup array is just the opposite. Does that make sense?

What I was asking is how the arrays are initially populated, and where are the vals coming from if not a sheet. Basically, I could not see for the moment at least, if you need a backup copy of either/both arrays while the code is running, why not just use extra 'backup' arrays when initially loading?

Mark
 
Upvote 0
Thanks guys, snb-I can't have them linked as one needs to change elements separately from the other after backup.
GTO-Both arrays are initialized the same at (200, 30).

So I decided to just loop it. And it actually is not so slow with this code. Might be faster code, but this rips pretty good for what I am doing, it seems:
Code:
Dim rr, cc
    rr = 1
    cc = 1
Do While rr <= 200
    Do While cc <= 30
        MyArray_BKUP(rr, cc) = MyArray(rr, cc)
        cc = cc + 1
    Loop
    cc = 1
    rr = rr + 1
Loop
Thanks again.
 
Upvote 0
I can't have them linked as

they are not linked.
You asked for a copy, you get an exact copy of the first array using my code.

Looping:
Code:
Sub snb()
  redim sq(300,20)
  sn=sq
  for j=0 to ubound(sq)
   for jj=0 to ubound(sq,2)
     sn(j,jj)=sq(j,jj)
   next
  next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

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