Using VBA to Copy and Paste a 2D Array in Excel

TG87

New Member
Joined
May 18, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have code I am developing where source data exists from column A to L starting at row 7. Separately, an output field based on that data exists in column M.

Goals of Code:
1) Import all data into an Array named "DataSet"
2) If any data exists in column M:
2a) Delete all data including output field in column M.
2b) Replace the original data in columns A through L.

Assumptions/Setup required for Code to Work:
1) Worksheets copied from have values in A1:A6 and M1:M6 (required for CountA as used).

Code:
VBA Code:
Sub Sample_2D_Array()

'ADDRESS SOURCE WORKSHEET'
Dim SheetName As String
SheetName = ActiveSheet.Name

'COPY AND PASTE DATASET'
Dim DataRows As Integer
Dim DataSet As Variant
'Import DataSet data into an array'
DataRows = Application.CountA(Range("A:A"))
DataSet = Range("A7:L" & DataRows)
'
Worksheets(SheetName).Activate
If Application.CountA(Range("M:M")) = 6 Then
'Do nothing - there is no old data'
Else 'Delete the helper column data'
Rows("7:" & Application.CountA(Range("M:M"))).EntireRow.Delete
ActiveSheet.Range("A7:L" & DataRows) = DataSet()
End If

End Sub

Why is it not working to assign the DataSet Array back exactly into the range it came from?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try it like
VBA Code:
ActiveSheet.Range("A7:L" & DataRows).Value = DataSet
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

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