How do I post an Array back into a new sheet?

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
Good afternoon, All, trying to learn how to make better use of arrays. I was following an online video and came up with this code. The problem is, I have no idea how to paste it back into a sheet!

VBA Code:
Sub FirstArray()
    
 Dim GL() As Variant
 Dim Dim1 As Long, Dim2 As Long

'Sheets.Add.Name = "Array"

Dim1 = Range("A6", Range("a5").End(xlDown)).Cells.Count - 1
Dim2 = Range("a5", Range("a5").End(xlToRight)).Cells.Count - 1

 ReDim GL(0 To Dim1, 0 To Dim2)
 
 For Dim1 = LBound(GL, 1) To UBound(GL, 1)
    For Dim2 = LBound(GL, 2) To UBound(GL, 2)
     GL(Dim1, Dim2) = Range("A6").Offset(Dim1, Dim2).Value
    Next Dim2
   Next Dim1
End Sub

I can see via the locals window that the array has data. How would I paste it back to a new sheet?

Thank you in advance.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Assuming there is a sheet named Array try something like this

VBA Code:
Sheets("Array").Activate
Range("A1").Resize(UBound(GL, 1) + 1, UBound(GL, 2) + 1) = GL

M.
 
Upvote 0
Solution
Assuming there is a sheet named Array try something like this

VBA Code:
Sheets("Array").Activate
Range("A1").Resize(UBound(GL, 1) + 1, UBound(GL, 2) + 1) = GL

M.
Thank you! This is exactly what I was looking for. Question, what exactly is the Resize property and how it is being used?

This is a code that I found on YouTube when trying to follow along an array tutorial, but it kept giving me an error. Admittedly, I have no idea what this code was even trying to do to begin with.

VBA Code:
For Dim1 = LBound(GL, 1) To UBound(GL, 1)
    For Dim2 = LBound(GL, 2) To UBound(GL, 2)
     GL(Dim1, Dim2) = Range("A6").Offset(Dim1, Dim2).Value
      ActiveCell.Offset(Dim1, Dim2).Value = GL(Dim1, Dim2)
    Next Dim2
   Next Dim1
 
Upvote 0
You can also load an Excel range directly into a Variant, rather than cell by cell as you have done. Much faster!

VBA Code:
Dim GL As Variant

GL = Worksheets("Sheet1").Range("A1:C10").Value

GL will now be a 10 x 3 array.

(Note that lbound will always be 1, regardless of whether you're using the default Base 0, or the Option Base 1 alternative).
 
Upvote 0
You can also load an Excel range directly into a Variant, rather than cell by cell as you have done. Much faster!

VBA Code:
Dim GL As Variant

GL = Worksheets("Sheet1").Range("A1:C10").Value

GL will now be a 10 x 3 array.

(Note that lbound will always be 1, regardless of whether you're using the default Base 0, or the Option Base 1 alternative).
Thank you for this. Which line would I change in my code in order to load to get this to work? Do I have to get rid of my Dim1 and Dim2 variables?

Also, if I had a dynamic range is there a way of letting Excel know to load all the date from Ranges A1 to Z10000 etc?
 
Upvote 0
Hope it help:

VBA Code:
Sub test()
Dim lr&, GL

' Get the last used row
lr = Range("A:Z").SpecialCells(xlCellTypeLastCell).Row

'read range value into variable array, named GL
GL = Range("A1:Z" & lr).Value

'Paste from AA100
Range("AA100").Resize(UBound(GL), UBound(GL, 2)).Value = GL

'Or overwrite from A1
'Range("A1").Resize(UBound(GL), UBound(GL, 2)).Value = GL
'Range("A1:Z" & lr).Value = GL


End Sub
 
Upvote 0
Hope it help:

VBA Code:
Sub test()
Dim lr&, GL

' Get the last used row
lr = Range("A:Z").SpecialCells(xlCellTypeLastCell).Row

'read range value into variable array, named GL
GL = Range("A1:Z" & lr).Value

'Paste from AA100
Range("AA100").Resize(UBound(GL), UBound(GL, 2)).Value = GL

'Or overwrite from A1
'Range("A1").Resize(UBound(GL), UBound(GL, 2)).Value = GL
'Range("A1:Z" & lr).Value = GL


End Sub
Thank you very much for this!
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
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