Inserting Rows based on Variable

tbakbradley

Board Regular
Joined
Sep 24, 2010
Messages
130
I have an Excel Spreadsheet built with 500 rows (Locations) beginning on Row 4 and ending on Row 504. There are Summations under it as well as Summary Tables. The User is able to select the number of Locations they require for the specific job, as long as it's not more than 500. If less than 500, the Macro will Delete the required number of Rows.

I have an old Spreadsheet tool I built that will Insert Rows, copy the row above it (with all the equations) and paste in the new Rows. However, it's VERY slow as I Recorded a Macro for the Code. FYI...I did the same to Delete rows and it was slow too until someone here provided a much simpler method.

I could use the slower method to insert Rows, but the problem I have is with column A which provides a "Location #" for easy sorting, etc. as different organizations will touch it later. Deleting from a completed spreadsheet isn't a problem keeping the "Location #s " correct. Inserting, I'm not sure about.

Here is the Code (Slow) that I have to insert Rows based on the number selected (this is an old Spreadsheet with 59 Rows/Locations already prebuilt. First Row of Data starts on Row 2, and the 59th Location is on Row 60). First....what is a better method to Insert a Row (so I do not mess up calculations at the bottom) and paste the equations from the row above? Second, allow for Column A to have the proper "Location #"? Today, Location # is "Location 1, Location 2, Location 3.......Location 59). Using this slow method, I would insert at Location 59 (Row 60), but it would no longer have the proper Location #s in Column A.

Note: "circuits" = the value the user enters when asked how many locations required.

Code:
ActiveSheet.Select    Sheets("IoF").Select
    NumbRows = circuits - 59
    For Counter = 1 To NumbRows
    Sheets("IOF").Select
    Rows("60:60").Select
    Range("C60").Activate
    Selection.Insert Shift:=x1Down
    Rows("59:59").Select
    Range("C59").Activate
    Selection.Copy
    Rows("60:60").Activate
    Range("C60").Activate
    ActiveSheet.Paste
    Range("C60").Select
    Next Counter
    End If
    Sheets("IOF").Select
    Application.CutCopyMode = False
    Range("D2").Select

Thank you so much for anyone that is willing to take the time to look at this.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I have an Excel Spreadsheet built with 500 rows (Locations) beginning on Row 4 and ending on Row 504. There are Summations under it as well as Summary Tables. The User is able to select the number of Locations they require for the specific job, as long as it's not more than 500. If less than 500, the Macro will Delete the required number of Rows.

I have an old Spreadsheet tool I built that will Insert Rows, copy the row above it (with all the equations) and paste in the new Rows. However, it's VERY slow as I Recorded a Macro for the Code. FYI...I did the same to Delete rows and it was slow too until someone here provided a much simpler method.

I could use the slower method to insert Rows, but the problem I have is with column A which provides a "Location #" for easy sorting, etc. as different organizations will touch it later. Deleting from a completed spreadsheet isn't a problem keeping the "Location #s " correct. Inserting, I'm not sure about.

Here is the Code (Slow) that I have to insert Rows based on the number selected (this is an old Spreadsheet with 59 Rows/Locations already prebuilt. First Row of Data starts on Row 2, and the 59th Location is on Row 60). First....what is a better method to Insert a Row (so I do not mess up calculations at the bottom) and paste the equations from the row above? Second, allow for Column A to have the proper "Location #"? Today, Location # is "Location 1, Location 2, Location 3.......Location 59). Using this slow method, I would insert at Location 59 (Row 60), but it would no longer have the proper Location #s in Column A.

Note: "circuits" = the value the user enters when asked how many locations required.

Code:
ActiveSheet.Select    Sheets("IoF").Select
    NumbRows = circuits - 59
    For Counter = 1 To NumbRows
    Sheets("IOF").Select
    Rows("60:60").Select
    Range("C60").Activate
    Selection.Insert Shift:=x1Down
    Rows("59:59").Select
    Range("C59").Activate
    Selection.Copy
    Rows("60:60").Activate
    Range("C60").Activate
    ActiveSheet.Paste
    Range("C60").Select
    Next Counter
    End If
    Sheets("IOF").Select
    Application.CutCopyMode = False
    Range("D2").Select

Thank you so much for anyone that is willing to take the time to look at this.

EDIT: Please note that the Location Numbers are actually just (1, 2, 3.....59). It does not say (Location 1, Location 2....Location 59).
 
Upvote 0
How about
Code:
        Sheets("IoF").Select
        numbrows = circuits - 59
        Range("A60").Resize(numbrows).Insert
        Range("C59").AutoFill Range("C59").Resize(numbrows + 1), xlFillSeries
    End If
    Range("D2").Select
This replaces all the code you supplied
 
Upvote 0
How about
Code:
        Sheets("IoF").Select
        numbrows = circuits - 59
        Range("A60").Resize(numbrows).Insert
        Range("C59").AutoFill Range("C59").Resize(numbrows + 1), xlFillSeries
    End If
    Range("D2").Select
This replaces all the code you supplied

Thank you.

I'm receiving some problems with this. Row 60 (#59) now also includes (59) in Column B (as well as Column A) and every other cell is shifted to the right in that row.

Also, the inserted Rows are not pasting the equations from the other rows. So, Column A will have all the right numbers (60, 61, 62, etc), but that's it. No other data on any other cell. Also, the "Summary Table that I have at the bottom of the the worksheet (Starting at Row 66) didn't move. So, all the new Numbers in Column A go down the Worksheet without the rest of the data in those rows, and it doesn't move my summary table down with it.
 
Upvote 0
Try this
Code:
        Sheets("IoF").Select
        numbrows = circuits - 59
        Range("A61").Resize(numbrows).EntireRow.Insert
        Range("A60").AutoFill Range("A60").Resize(numbrows + 1), xlFillSeries
        Range("B60:[COLOR=#ff0000]E[/COLOR]60").Resize(numbrows + 1).FillDown
    End If
    Range("D2").Select
Change E to match your last column
 
Upvote 0
Try this
Code:
        Sheets("IoF").Select
        numbrows = circuits - 59
        Range("A61").Resize(numbrows).EntireRow.Insert
        Range("A60").AutoFill Range("A60").Resize(numbrows + 1), xlFillSeries
        Range("B60:[COLOR=#ff0000]E[/COLOR]60").Resize(numbrows + 1).FillDown
    End If
    Range("D2").Select
Change E to match your last column

Incredible. This works!!!!! thank you so much. I'm going to take this now and build it into a Macro in another Tool I've developed and hopefully get it working as well.

Thank you so much!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Glad to help & thanks for the feedback

Well, this is a little embarrassing, but although I was able to alter the code of my old tool to insert rows more efficiently with the code above, and it worked, I'm having difficulty on my newest Tool.

Much like the last one, but Location "1" begins on Row 4 and Location "500" ends on Row 503. Column A is the Location Number, 1-X (500 to start). I have data from Columns A through IG. There Column Totals on Row 504 and Summary Tables beginning at row 505.

Based on your code yesterday that I had working on my old tool, I can't get it here and I'm sure I'm overlooking something simple.

Code:
If locations > 500 Then
    Columns.EntireColumn.Hidden = False
    numbrows = locations - 500
    Range("A504").Resize(numbrows).EntireRow.Insert
    Range("A503").AutoFill Range("A503").Resize(numbrows + 1), xlFillSeries
    Range("B503:IG60").Resize(numbrows + 1).FillDown
    End If

Running the Macro builds the proper number of rows and continues with the proper Location Numbering, but none of the new rows contain any of the Equations/Data like the first 500 locations.
 
Upvote 0
Try the change in red
Code:
    Range("B503:IG[COLOR="#FF0000"]503[/COLOR]").Resize(numbrows + 1).FillDown
 
Upvote 0
Try the change in red
Code:
    Range("B503:IG[COLOR="#FF0000"]503[/COLOR]").Resize(numbrows + 1).FillDown

Like I said...embarrassing!!! That was it. I looked at that code 15 times trying to figure out why it wouldn't work.
 
Upvote 0

Forum statistics

Threads
1,215,492
Messages
6,125,116
Members
449,206
Latest member
burgsrus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top