Help with macro code

ctc005

New Member
Joined
Jul 20, 2007
Messages
15
Ok, the following is a macro I'm using to insert and expand two separate sections of my worksheet. The problem is that both of these two spots that I'm expanding reference one another so when I expand if cell E9 is greater than 1 my cell referencing gets screwed up. Is there a way to tell it to expand one row at a time in each spot? So starting in row 10 it would insert and expand one row, then it would insert and expand at row 17, then it would jump back to now row 11 insert and expand, then jump to row 18 insert and expand.... etc. I would need this repeated the number of times represented in cell E9. Any help is extremely appreciated.

Code:
Sub InsertRowsAndFill()
Cells(11, "A").Resize(Range("E9").Value - 1).EntireRow.Insert
Cells(10, "A").EntireRow.Copy Cells(11, "A").Resize(Range("E9").Value - 1)

Cells((18 + Range("E9")), "A").Resize(Range("E9").Value - 1).EntireRow.Insert
Cells((17 + Range("E9")), "A").EntireRow.Copy Cells((18 + Range("E9")), "A").Resize(Range("E9").Value - 1)
End Sub
 

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,795
Office Version
  1. 2019
Platform
  1. Windows
I took a different approach...now that you are inserting only one row at a time, resize doesn't seem to add much, so I just inserted the row and copied the row above. I hope I have the logic right for you...let me know if this works.

Code:
Sub InsertRowsAndFill()
Dim x As Integer, y As Integer
y = 0

For x = 1 To Range("E9").Value - 1
    
    Rows(11).EntireRow.Insert
    Rows(10).Copy Destination:=Rows(11)
    
    Rows(18 + y).Insert
    Rows(17 + y).Copy Destination:=Rows(18 + y)
    
    'an extra variable y is used to keep pace due to
    'rows being inserted above at Rows(11)
    y = y + 1

Next

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,158
Messages
5,600,054
Members
414,357
Latest member
Gemma_R

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
Top