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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

860 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