?
Solved

Is there a more efficient method of inserting new row (with same formatting as prior rows) into a specific area of worksheet?

Posted on 2011-02-15
5
Medium Priority
?
218 Views
Last Modified: 2012-05-11
I have a worksheet that is structured to allow multiple users to enter part-level detail.  The part detail section is in the lower half of the worksheet.  I have provided users with an Insert Rows command button.  

I want to know if the approach I have taken is the most efficient...are there other VBA methods or functions that would speed up the code.  User could add as many as 5000 rows.  The worksheet provides has one (1) unlocked row for input, by default.
NumRows = Application.InputBox("How many rows do you wish to add?", Type:=1)
    UpperBound = NumRows
    
'Display if user chooses Cancel button
    If NumRows = False Then
        MsgBox "Add function cancelled", vbInformation
        Application.EnableEvents = True
        Exit Sub
    End If

'Add number of rows specified
    lastRow = findLastRows
    For counter = 1 To UpperBound
        Rows(lastRow + 1).Select
        Selection.Insert shift:=xlDown
        lastRow = lastRow + 1
        
        newRow = Rows(lastRow).Row
        ReDim Preserve AddNewRowArray(UpperBound)
        AddNewRowArray(counter) = newRow

        Rows(lastRow - 1).Copy
        Rows(newRow).PasteSpecial _
           Paste:=xlPasteFormats
    Next counter

Open in new window

0
Comment
Question by:BenniBoy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 34901688
Haven't tried it, but it seems it might be easier (although maybe not more efficient) to select the entire block at once and paste, rather than one row at a time.

So replace the loop
     For counter = 1 To UpperBound
with a copy/select/paste like this:

        Rows(lastRow).Copy
        Range("A" & (lastRow + 1) & ":Z"& (lastRow + UpperBound)).Select
        Selection.Insert shift:=xlDown
        Rows(newRow).PasteSpecial _
           Paste:=xlPasteFormats

0
 

Author Comment

by:BenniBoy
ID: 34909272
Well, I am providing the user with the option to specify the number of rows to insert...could be any number (within the constraint of rows allowed by Excel); so not sure if copying a block and then inserting will be an improvement...
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 2000 total points
ID: 34909491
I'd think that inserting a block of rows rather than 1 row at a time allows Excel to work more efficiently for that one step.

For example,  if row 20 needs to have 5 rows inserted before it, the loop routine would have to rename row 20 to row 21 (and rename every row below it) then rename 21 to 22 (and every row below it) then 22 to 23, 23 to 24 and 24 to 25.
With the single insert of 5 row, I would expect Excel to rename row 20 directly to 25 (and rename every row below it by an increment of 5) then insert the 5 blank rows.

Similarly, when you copy one row and paste it, there's a bit of overhead to put it into the clipboard and take it out.  Doing that five times will typically take more effort than doing it one time with a larger block of data.

If you're having performance issues, there are several things to look at, and things inside loops are usually the first to be examined.  For example, do you really need to copy the lat row every time through the loop?  Won't it be the same data every time?  Maybe you can move it outside the loop and copy it only once but paste it each time inside the loop.
0
 
LVL 24

Expert Comment

by:broomee9
ID: 35225321
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question