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
212 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
  • 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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

808 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