Link to home
Create AccountLog in
Avatar of Bob Stamm
Bob StammFlag for United States of America

asked on

Load Array with Values on Excel worksheet

Looking to load array in Excel.  Will be assigning this code to a command button.

When selecting the command button all the values will be loaded into an array.

Sheet1
values D5 to ND5

I am just getting back into programming and need an model to get me going.  Thanks to anyone that can assist.
Avatar of Norie
Norie

Quite straightforward really.
Dim arrData As Variant

    arrData = Sheets("Sheet1").Range("D5:ND5").Value ' fill array with data from N5:D5

Open in new window


Note the above code will produce a 2-dimensional array with the dimensions 1 to 1, 1 to 365, i.e. 1 row and 365 columns, if you like.
Avatar of Bob Stamm

ASKER

Thank you...that has me going.  I tosses in a msgbox to make sure I was getting the data loaded.  

thanks again and for the article too.
I now want to place my data on the "Print Calendar" worksheet.  The fill data array captured all the data for 365 days (works great).  I just want to place the first 31 days of data on the "Print Calendar" worksheet.  I assuming I need some type of loop or is there a more direct statement?  

Dim arrData As Variant

    arrData = Sheets("Calendar").Range("D5:ND5").Value ' fill array with data from D5:ND5

'This statement I wrote places the same value in each cell (I need help here).  
    Sheets("Print Calendar").Range("B55:AF55").Value = arrData(1, 2) ' Place calendar data for January
   
End Sub
Try this.
    Sheets("Print Calendar").Range("B55").Resize(,31).Value = arrYear

Open in new window

Note, I've used Resize(,31) to expand the destination range for the data by 31 columns.
Starting to get there.  If I need to grab from 32 to 60 out of arrData how is that done?  This would be for my month of February.

Dim arrData As Variant
    arrData = Sheets("Calendar").Range("D5:ND5").Value ' fill array with data from D5:ND5

    Sheets("Print Calendar").Range("B55").Resize(,31).Value = arrData ' January Data
    Sheets("Print Calendar").Range("B56").....???                                       ' February Data
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
This worked great.  Thank you for your help.