Results 1 to 17 of 17

Running a For Loop within another For Loop

Threaded View

  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):

        For i = 2 To CSFinalRow
            PCode = ""
            For j = 2 To ODFinalRow
                If CS.Cells(i, 3) = OD.Cells(j, 1) Then
                    If PCode = "" Then
                        PCode = OD.Cells(j, 11)
                    Else: PCode = PCode & " " & OD.Cells(j, 11)
                    End If
                End If
            CS.Cells(i, 4) = PCode
            Next j
        Next i

    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.

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