Results 1 to 11 of 11

Assigning array values with a loop

Threaded View

  1. #1
    Registered User
    Join Date
    12-09-2016
    Location
    USA
    MS-Off Ver
    2016
    Posts
    8

    Assigning array values with a loop

    So I'm working on a project to keep track of all the supplies I need to keep on hand in Mr. Rogers Neighborhood to keep all of my animals alive. I have hundreds of different sheets and I'm trying to just be able to input all of my values on 1 sheet and send them to their respective sheets, with the sheet name in column A and the destination range which I can update depending on what row I want to send the data to. (All of the data will be pasted in the same row and column of each respective worksheet.) This is the code I'm using that works:

    Private Sub CommandButton1_Click()
    Dim beg As String
    Dim fin As String
    Dim animal(2 To 5) As String
    beg = Range("J2").Value
    fin = Range("K2").Value
    animal(2) = Range("A2").Value
    animal(3) = Range("A3").Value
    animal(4) = Range("A4").Value
    animal(5) = Range("A5").Value
    Sheets("Supplies").Range("B2:E2").Copy Destination:=Sheets(animal(2)).Range(beg, fin)
    Sheets("Supplies").Range("B3:E3").Copy Destination:=Sheets(animal(3)).Range(beg, fin)
    Sheets("Supplies").Range("B4:E4").Copy Destination:=Sheets(animal(4)).Range(beg, fin)
    Sheets("Supplies").Range("B5:E5").Copy Destination:=Sheets(animal(5)).Range(beg, fin)
    End Sub
    It wouldn't be too difficult to write the code for hundreds of sheets by just concatenating a couple of different columns in Excel but I'm trying to learn VBA so I've been attempting to figure out a loop to assign values to the array:

    for i= 2 to 5
    range(1,i).value=dept(i)
    next i
    and then a loop for the copy, paste function:

     for j= 2 to 5  
    Sheets("Supplies").Range(cells(2,j),cells(5,j)).Copy Destination:=Sheets(animal(j)).Range(beg, fin)
    next j



    What I picture this code doing during the first i loop is taking the value in "A2" which is "cat" and make that value = dept(2)
    second loop takes the value of "B3" which is "dog" and making that value = dept(3)
    and once I have all of those values assigned it moves on to the j loop which I'm thinking (for the first iteration) copies the values in the range B2:E2 in the supplies sheet and then pastes it in the worksheet animal(2) which has been assigned the value of "cat" in the previous loop into the range that I've specified in beg and fin.

    Alas, Mr. Rogers keeps running into range issues and now all of his wonderful critters are at risk of slowly and painfully withering away unless you wonderful people can help him figure out how to make his code work. Thanks in advance for all of your help!
    Attached Files Attached Files
    Last edited by Mr. Rogers; 12-14-2016 at 11:59 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. help assigning elements of dictionary to array and then pasting array into worksheet
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-05-2013, 10:37 AM
  2. Assigning values to names in a for next loop
    By GazzaLDN in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-15-2013, 04:51 PM
  3. [SOLVED] Loop fail when assigning values to multirow ranges
    By Hester's Dad in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-23-2012, 01:30 PM
  4. Assigning values into array using the "Array" vba function.......not working
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-17-2011, 04:21 PM
  5. Problem assigning my Array values to rows
    By Edoloto in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-17-2008, 08:14 PM
  6. [SOLVED] assigning values to array
    By Juggernath in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-26-2005, 03:25 PM
  7. assigning values to an array
    By Juggernath in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-24-2005, 11:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1