insert x rows using input box between data only when value in column B changes

JMoves

New Member
Joined
Jan 2, 2020
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
I am trying to using an input box to ask "how many rows to be entered?" then insert X number of blank rows between existing data only when the value changes in column B.

For example, user states 5 in the input box - then 5 rows would be inserted between Row 5 and 6, Rows 8 and 9, Rows, 11 and 12, etc.

I can insert one row using the code below, however would like the number to be variable. If I try to run it more than once it provides more than one row beyond the second execution.

Sub InsertRowAtChangeInValue()
Dim lRow As Long
For lRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row To 4 Step -1
If Cells(lRow, "B") <> Cells(lRow - 1, "B") Then Rows(lRow).EntireRow.Insert
Next lRow
End Sub

Any help would be greatly appreciated.
 
Well I did not realize you wanted the same number of rows inserted each time.
So glad to see you know how to modify scripts to do what you want.
I always like it when users can understand the code and can modify it if needed.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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