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
210 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now