+ Reply to Thread
Results 1 to 3 of 3

Need Macro to insert a Specific number of Rows (in Filtered list)

  1. #1
    Registered User
    Join Date
    11-05-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    56

    Need Macro to insert a Specific number of Rows (in Filtered list)

    I'm trying to insert a specific # of rows in a filtered list, for this example lets say 30.

    I want my code to drop down 6 rows from the active cell, then insert Shift:=xlDown by 30 rows

    My Code so far:
    Please Login or Register  to view this content.
    Only works if there are no filtered/hidden rows in the range defined by the above code "ActiveCell.Offset(6, 0).Range("A1", "A" & RowsToInsert)", or just "ActiveCell.Offset(6,0).range("A1:A30") ". If there are filtered out rows then it only inserts the visible number of rows in the defined range. So in this example since i've filtered out two, 6 row workpackages in the range, it will only insert 18 rows instead of 30.

    I've also tried using Rows("1:30") instead of range(...) but that also produces the same result as using the range method. So the selecting a range, then inserting rows method seems to not work consistently with filtered lists and I need a work around.

    Please see my attached work book and run or step into the InsertRows macro while A8 is the active cell to see what i mean.

    The intent of this is to replace the For Next loop in my "add work package" macro that adds rows. Rather than inserting blocks of 6 rows at a time and looping which starts to take longer the larger and larger the sheet gets, i want to just insert the exact number of rows I need all at once which is much faster.

    Right now the only alternative I can think of is a Do Until Loop that resizes the range selected until the number of visible rows in my selection = the number or rows i want to insert, however I'm hoping there is a faster, simpler and more elegant solution out there i'm missing other than using a loop.
    Last edited by JTwrk; 06-08-2012 at 04:50 PM.

  2. #2
    Registered User
    Join Date
    05-10-2011
    Location
    Miami
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Need Macro to insert a Specific number of Rows (in Filtered list)

    I am here far more often for help then to give answers, and the elegance of this code reflects that, but it does do what you ask it to:
    Please Login or Register  to view this content.
    Hope that helps!

  3. #3
    Registered User
    Join Date
    11-05-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Need Macro to insert a Specific number of Rows (in Filtered list)

    Thank you for the reply. The problem is that still executes the insert function multiple times, i could be inserting anywhere from 6 rows to 12000 rows (or more) depending on how many workpackages I want to look at, at once.

    I was doing 6 rows at a time and looping for each workpackage i wanted to add, which worked fine except as i added more and more rows each subsequent insert of rows took longer. So for inserting say 200 workpackages it may have only took 10 seconds but inserting 1000 work packages took significantly longer even with screen updating off. Longer than just 5 * the time it took to insert 200 work packages.

    The faster solution is to insert all the rows needed for the workpackages at once and then loop through the copying of the empty workpackage range to each row, skipping down i * (count rows in workpackage range) for each subsequent workpackage insert.

    Anyways I wish there was a more elegant solution but this seems to do the trick for now, haven't fully bug tested it yet though:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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