+ Reply to Thread
Results 1 to 17 of 17

Running a For Loop within another For Loop

  1. #1
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Atlanta
    MS-Off Ver
    Excel 2007
    Posts
    171

    Running a For Loop within another For Loop

    Hello,

    I am working on consolidating data from a report and I need help finding a faster way to do what I'm doing.

    I have a database full of customer numbers - each customer number will appear one or more times in the list. I also have a product code for each line item (which means I could have many product codes for each unique customer number). I'm trying to put together a tab with consolidated information - for example, one column that contains each unique customer number and a corresponding column that contains all of the product codes for the unique customer number.

    I already have code that does this, but the original database (the one that contains repeating customer numbers) can contain up to 20,000 line items. Which means, for each unique customer number in the consolidated records, I will have to check each of the 20,000 records for Product code matches. This seems to take much longer than I expected.

    Here is the code I have (from the original macro - I have adjusted accordingly for the sample file attached):

    Please Login or Register  to view this content.

    However, there is a lot more to the code than just this piece. So, I have provided a sample file that uses simplified data and code. Please let me know if you can think of a way to speed up the process.

    Also, there is one more thing I want to throw out there - sometimes, when I run a macro on my computer that I've also written, it takes a long time for certain things to happen (I don't know why). So, if the macro runs fairly quickly when you test it, then I may not need additional assistance. If this is the case, please just let me know.

    Thanks!
    Attached Files Attached Files
    Last edited by ATLGator; 01-19-2014 at 08:23 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Running a For Loop within another For Loop

    Hi

    Sorry the server won't let me upload files. So bear with me.

    I would take a fresh look at this.

    I put the customer name in column A and the product code in column B [ Use row 3 and downwards ]

    I created a list of customers in row 1 [ See Below ]. Starting at C1

    To speed things up you need to use excels built in functions wherever possible.

    For example to get a list of unique customer numbers. Duplicate the list and use the remove duplicate data function in the data tab.

    Now you need to map all the products that each customer has bought

    Select the list of customers paste into another column, use remove duplicates to get did of duplicates
    Copy and paste transpose to paste the customer names into one row.

    A simple formula finds all the products used by each customer.
    Please Login or Register  to view this content.
    I haven't done it but you could use remove duplicates for each customer to reduce the list.

    Then use my new concat function to concatenate the data.

    Please Login or Register  to view this content.
    Last edited by mehmetcik; 01-19-2014 at 09:14 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Running a For Loop within another For Loop

    Hi -

    Your loop inside loop codes will repeat 7079 records in 2747 times.

    try this one;
    Please Login or Register  to view this content.
    event

  4. #4
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Atlanta
    MS-Off Ver
    Excel 2007
    Posts
    171

    Re: Running a For Loop within another For Loop

    event12 - I dont know what you did, but it worked perfectly. Thank you!! On a side note, is there anything you can point me to so that I can understand how the "scripting.dictionary" is being used? I've never worked with that before and it seems like it would be a helpful tool.

  5. #5
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Running a For Loop within another For Loop

    Hi -

    My username is event21 not event12.
    you can start from MSDN

    http://msdn.microsoft.com/en-us/libr...=vs.84%29.aspx

    event

  6. #6
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Atlanta
    MS-Off Ver
    Excel 2007
    Posts
    171

    Re: Running a For Loop within another For Loop

    event21 - sorry about that . . .

    I've actually got one more thing I need to throw into the mix. It seems I oversimplified my sample file. For the Original Data tab, the colors in the sample file (which are in col B, next to the customer number) are actually in Col K on the real file, separated from the Customer Number by 10 columns. Also, the resulting consolidation in the second tab occurs in Col C & D, not A & B like in the first file.

    I've added a new sample file to reflect these changes that includes the code you've already sent me, with some minor changes to adjust the final placement of the data (Col C & D), but I can't figure out how to grab Col A & Col K from the original data tab.

    Please Login or Register  to view this content.

    I thought it was just a matter of changing the offset number from 1 to 10, but that gives me an error.

    Can you guide me once more?

    Thanks!!!!
    Attached Files Attached Files
    Last edited by ATLGator; 01-21-2014 at 07:40 PM.

  7. #7
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Running a For Loop within another For Loop

    Hi -

    What is the error for this after changing the offset?
    Please Login or Register  to view this content.
    event

  8. #8
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Atlanta
    MS-Off Ver
    Excel 2007
    Posts
    171

    Re: Running a For Loop within another For Loop

    No error . . . I dont know what I did wrong originally, but replacing what I had with fresh code worked perfectly. Thanks again!!

    Also, what does

    Please Login or Register  to view this content.
    contribute to? I'm studying the code to learn it and dont see those variables used?

  9. #9
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Running a For Loop within another For Loop

    Hi -

    You can delete those variables, it was just put in there previously but no used at all

    event

  10. #10
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Atlanta
    MS-Off Ver
    Excel 2007
    Posts
    171

    Re: Running a For Loop within another For Loop

    Ok, thanks again!!!!

  11. #11
    Registered User
    Join Date
    06-23-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Running a For Loop within another For Loop

    This looks like something I may need. I have tried using the code posted by event21 but it keeps throwing an error.

    Please see an earlier thread I posted this morning here:
    http://www.excelforum.com/excel-form...ml#post3554674

    Many thanks

  12. #12
    Registered User
    Join Date
    06-23-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Running a For Loop within another For Loop

    Anyone????

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,718

    Re: Running a For Loop within another For Loop

    @RandomlySet : Anyone what? I think you should be bumping your own thread ... and looking at it ... to see that there have been replies which address your question.

    Your posts in this thread could be considered a) hijacking and b) unnecessary

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  14. #14
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Atlanta
    MS-Off Ver
    Excel 2007
    Posts
    171

    Re: Running a For Loop within another For Loop

    Actually, I unmarked this thread as Solved because I am running into an error too (maybe the same as RandomlySet).

    When I first open the sample worksheet and apply the code, it works fine. However, when I run it again after making changes to the code that do not affect the dictionary, I get a "Run-time error '13': Type mismatch" error when I try to transpose the items. Does anyone have an idea why this is happening?
    Last edited by ATLGator; 01-22-2014 at 11:57 AM.

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,718

    Re: Running a For Loop within another For Loop

    Look at the other thread: http://www.excelforum.com/excel-form...ml#post3554813

    Probably the same issue if you have a lot of data.

    Regards, TMS

  16. #16
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Atlanta
    MS-Off Ver
    Excel 2007
    Posts
    171

    Re: Running a For Loop within another For Loop

    My issue was resolved by replacing the transpose function
    Please Login or Register  to view this content.
    with this
    Please Login or Register  to view this content.
    I really love this community!!

  17. #17
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,718

    Re: Running a For Loop within another For Loop

    You're welcome. Thanks for the rep.


    The issue is resolved by replacing that part of the code and is caused by the volume of data being collated and then dumped (transposed) into the cells.


    Again, as I said there, the credit is down to event21 ... all the clever work is his. I just tweaked it a little.


    Regards, TMS

+ 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. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  2. If Valua A = Value B... If Value C = Value A without running through first loop...
    By ThomasCarter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-18-2012, 01:25 PM
  3. Do loop not running
    By hattrick_123a in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-21-2011, 08:36 PM
  4. Loop not running
    By T De Villiers in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2006, 10:26 AM
  5. Loop running really slow...?
    By Buffyslay in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-26-2006, 09:40 AM

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