+ Reply to Thread
Results 1 to 11 of 11

Assigning array values with a loop

  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:

    Please Login or Register  to view this content.
    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:

    Please Login or Register  to view this content.
    and then a loop for the copy, paste function:

    Please Login or Register  to view this content.



    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.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Assigning array values with a loop

    Any chance you could upload a sample workbook?

    Click on GO ADVANCED, scroll down and click Manage Attachments.
    If posting code please use code tags, see here.

  3. #3
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Assigning array values with a loop

    If you can upload a example workbook with explanation of what you want will help you get a solution faster.

    Please Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the [#] button at the top of the post window (if you are editing an existing post, press Go Advanced to see the [#] button). The result will appear like this in the post window:

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

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

    Re: Assigning array values with a loop

    Sorry about that! I added the code tag and uploaded an example. It's got the long-form, working version of code in it currently.

  5. #5
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Assigning array values with a loop

    Give this a try. Don't think arrays are needed with what your doing. But if you would like me to I could give you an example with arrays.

    Please Login or Register  to view this content.

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

    Re: Assigning array values with a loop

    Worked perfectly! Thank you so much! But just for my own knowledge, is there a way to assign values to an array with a loop? For instance, if I have "cat" in A1 (want to assign it animal (1) and "dog" in A2 (want to assign it animal(2), is there a way to make a loop that says: loop 1 (i=1)--value in A, i = animal (i) and then for the second loop (i=2)-- value in A,i =animal (i) and then be able to use those assigned arrays further down in my code? Again, thank you so much!

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Assigning array values with a loop

    here is a couple of ways to do what your doing. For what your doing you don't need a loop to assign the values. See if this helps

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    05-02-2015
    Location
    calgary alberta
    MS-Off Ver
    2012
    Posts
    205

    Re: Assigning array values with a loop

    FYI the best intro to a post I have seen yet

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

    Re: Assigning array values with a loop

    Again, thank you so much for taking the time to write this. I've just got one question: I defined
    Please Login or Register  to view this content.
    so that I can put a value in them (ex. J2= "H5" & K2= "K5")which will define the range that the data will be posted to on their respective pages.
    Please Login or Register  to view this content.
    With your code it posts to the next available row. Is there a way to use that "with" statement to paste to the values that I specified in J2 and K2?

    And thanks Sparky! I figure if I'm asking for your help and taking your valuable time away from other things you could be doing I should at least try and inject a little joy and humor in it.

  10. #10
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Assigning array values with a loop

    Like this?

    Please Login or Register  to view this content.
    Last edited by mike7952; 12-13-2016 at 11:11 AM.

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

    Re: Assigning array values with a loop

    Just like that! Worked perfectly! Thank y'all again for all of your help!

+ Reply to Thread

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. 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