+ Reply to Thread
Results 1 to 5 of 5

Loop Prevention: Duplication/Deletion of Rows

  1. #1
    Registered User
    Join Date
    10-04-2012
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    18

    Loop Prevention: Duplication/Deletion of Rows

    Hello Everyone,

    I'm hoping that there is someone a bit more creative than me out there who can help figure out a way to optimize this loop or get rid of it all together!

    The loop in question is part of a larger procedure whose general intent is to transform poorly formatted data into pivot-table optimized data. The data set can have in excess of 75k lines and the order of the data and amount of data is always changing. The loop (which generally takes over 5 minutes to run as-is), represents just one step in the procedure, but about 90% of the time it takes.

    Here is what the loop does: Certain lines in the data are tagged with a "Yes" indicating that value of these lines needs to be split up into three groups, determined by 3 cell values adding up to 100%. My loop checks each line for a yes, and if there is one creates three copies of the line at the bottom of the data (using the % values to split up the amount among the lines). It then goes back and deletes the original line so the value is not double counted and the data is optimized for a pivot table (as many individual records [rows] as possible). To help clarify this description better, I have attached a file with some dummy data that represents exactly what the loop is doing my real file. The code is also pasted below. Any ideas on how to optimize this code would be truly appreciated!!

    Thank you in advance.
    (P.S. – The Timer is just there to keep track of how long this is taking, and the intention would be to delete columns G:l in the data at a later point.)
    Please Login or Register  to view this content.
    Attachment 303384
    Attached Files Attached Files
    Last edited by mlj61289; 03-10-2014 at 05:27 PM. Reason: Updated Attachment

  2. #2
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Loop Prevention: Duplication/Deletion of Rows

    Hi..

    Instead of looping through your entire data range and testing for a "Yes" using the IF statement.. maybe autofilter Column F first using "Yes" as the Criteria and then loop through that.. by the looks of your test data... that would cut the loop down by about 75%...

  3. #3
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    328

    Re: Loop Prevention: Duplication/Deletion of Rows

    You could use a do while true loop with a find "yes" statement (find is quicker than for each). if you really want to shorten it you can have the find range shrink each loop based on where the last result was found.
    maybe something like
    Please Login or Register  to view this content.
    Last edited by Leon V (AW); 03-12-2014 at 06:15 AM.
    Sub Reputation()
    Dim Problem as Variant
    Dim Reputation as Integer
    For Each Problem in Forum.Threads
        If Problem.Title = "*[Solved]*" and Solver.Name = "Leon V (AW)" Then Reputation = Reputation + 1
    Next Problem
    End Sub

  4. #4
    Registered User
    Join Date
    10-04-2012
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Loop Prevention: Duplication/Deletion of Rows

    Hello Apo, Leon,

    Thanks for the ideas, I tried each of them and each reduced the time quite a bit - but while I was tinkering I actually found a solution that brought the time down to almost nothing. Rather than looping through each row of the 75k lines of data and creating a copy one by one, the procedure sorts the data ascending, and then uses a find to give me the first row containing a yes. It then creates three copies of all the "Yes" rows, replacing whats in Column D with the new values from Col J, K, and L, and re-tagging Col F on the new rows to "No". Then I re-sort the data ascending and simply delete the original "Yes" lines. On my full dataset, this piece of the procedure now takes 8 sec on average - compared to 5 Min before!

    I didnt update my sample code file with the new procedure, but could if anyone was interested. Just let me know.

    Thanks for your help guys!

    P.S. - Leon - best signature ever.

    Mike

  5. #5
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    328

    Re: Loop Prevention: Duplication/Deletion of Rows

    Have Microsoft enabled custom sorting algorithms in excel yet? (I know they haven't in 2007) as you only have two values you could cut the time from N Log N + ? down to just N with a custom soft algorithm.
    Last edited by Leon V (AW); 03-18-2014 at 06:03 AM.

+ 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. Can't get loop to work correctly, skips rows after deletion
    By wyanrandy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-26-2014, 06:30 PM
  2. [SOLVED] Data validation, drop down lists and duplication prevention?
    By Johnny247 in forum Excel General
    Replies: 11
    Last Post: 12-20-2013, 10:29 AM
  3. How to merge Rows without Duplication
    By manmeets1 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-23-2012, 05:47 PM
  4. Replies: 1
    Last Post: 06-14-2012, 09:27 AM
  5. condition based deletion - using loop
    By webbug08 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-02-2009, 07:36 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