[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
?
221 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
4 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:Tracy
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

872 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