+ Reply to Thread
Results 1 to 5 of 5

For Each Loop Macro

  1. #1
    Registered User
    Join Date
    09-20-2012
    Location
    Oklahoma
    MS-Off Ver
    Excel 2007
    Posts
    35

    For Each Loop Macro

    I have included a sample file with a basic example of an input and the desired output after the macro has ran. This list will be dynamic on the input tab Col A and Col C will only be around 4-5 lines long but I would want the macro to be able to handle more if needed in the future. Col B however will range from 1-75,000+ again would want it to be able to handle any size.

    The basic idea is that the info in Col A&C will be duplicated for each entry in Col B. The data in Col A&C are paired so as in the example A will always be with X, B always with W, and C always with Z.

    I get the feeling this can be done with a for each loop but i am having "writer's block"/"brain fart" in figuring out how to actually code it.

    I am using Excel 2007 on Windows XP if you need that info.

    All help is appreciated and please let me know if there is any more info you need.

    Sample file.xlsx
    Last edited by chad portman; 01-10-2014 at 12:48 AM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: For Each Loop Macro

    Try the attached.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    09-20-2012
    Location
    Oklahoma
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: For Each Loop Macro

    For some reason when I click the button you provided it tries to reopen the file and then there is a cannot run the macro error stating the marco may not be available in this workbook or all macros may be disabled. I have the setting to allow macros set so that is not the issue. However when i open the VBA editor and use F8 to step through it the process appears to work prefectly. I have to do some research now on Ubound as i have not seen that before and if i need to adjust this for more Col or something of that nature i need to be able to modify the code as needed.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: For Each Loop Macro

    Hello chad portman,

    The macro below has been added to the the attached workbook. There is a button on the "Desired Output" to run it. It uses 2 For Each Next loops to create the "Desired Output".

    While this macro works with your sample data, it may not with your actual data unless the following conditions apply...
    1. There are no column headers used.
    2. There is at least one blank row and column around the input and output data areas

    Macro Code
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    09-20-2012
    Location
    Oklahoma
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: For Each Loop Macro

    AB33 thank you for your response and once i understand arrays a little better i think i will like your solution the best. As i have to adapt this my real data and I am having issues doing that at the moment with your solution. It just feels cleaner though so i will keep working on it.

    Leith Ross your solution is very simplistic but works and I am able to adjust it to my data set so thank you for your response. I do have concerns about the fact that it loops through for every blank cell of col a that is next to a non blank cell of col b doing nothing in each loop. I fear that with col a only being 4 long and col b being 75000 or more that it could be a resource hog.

+ 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. Novice with VBA and macro writing needing help to loop a macro unti no data
    By jrongone in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-25-2013, 12:19 PM
  2. Help writing a macro
    By Braddigan in forum Excel General
    Replies: 0
    Last Post: 06-28-2011, 10:47 AM
  3. Writing a macro (copy&paste macro)
    By skatmandu2002 in forum Excel General
    Replies: 3
    Last Post: 02-23-2010, 06:26 PM
  4. Help writing a macro
    By IcarusFB in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-31-2005, 07:50 PM
  5. Macro Writing
    By rjamison in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2005, 08:05 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