populating an array from external data

kualjo

Board Regular
Joined
Aug 15, 2006
Messages
118
Office Version
  1. 365
Platform
  1. Windows
I'm not too good with arrays (yet), but am beginning to suspect that they are intended to be sources of data and not destinations. I have a column of data that is based on sequential dates that I need to put into a calendar-style orientation, i.e., 7 days across, then on to the next week. My code currently looks like this:

Code:
Const maxr = 52, maxc = 7
Dim data_range(1 To maxr, 1 To maxc) As Variant

data_range = Range("D3:K54")

For r = 1 To maxr
    For c = 1 To maxc
        data_range(r, c) = ActiveCell.Value
        ActiveCell.Offset(1, 0).Activate
    Next c
Next r

data_range is the cell range where I want the numbers to go. The existing column of data is not referenced in this bit of code, but it is the ActiveCell.Value.

So what am I doing wrong here?
 

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.
Instead of:

Code:
Dim data_range(1 To maxr, 1 To maxc) As Variant
data_range = Range("D3:K54")

try:

Code:
Dim data_range As Range
Set data_range = Range("D3:K54")
 
Upvote 0
I think that you declare at first your range as D3:K54 and then with the for-next syntax you place the value to cell A1, since r=1 and c=1

George

I'm not too good with arrays (yet), but am beginning to suspect that they are intended to be sources of data and not destinations. I have a column of data that is based on sequential dates that I need to put into a calendar-style orientation, i.e., 7 days across, then on to the next week. My code currently looks like this:

Code:
Const maxr = 52, maxc = 7
Dim data_range(1 To maxr, 1 To maxc) As Variant
 
data_range = Range("D3:K54")
 
For r = 1 To maxr
    For c = 1 To maxc
        data_range(r, c) = ActiveCell.Value
        ActiveCell.Offset(1, 0).Activate
    Next c
Next r

data_range is the cell range where I want the numbers to go. The existing column of data is not referenced in this bit of code, but it is the ActiveCell.Value.

So what am I doing wrong here?
 
Upvote 0
That worked Andrew. I knew it would be something simple. Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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