Excel VBA Insert Row Code Works - How to Update Code to Include Formulas and Formatting?

slivesay

Board Regular
Joined
Jan 4, 2019
Messages
64
I have the below code that uses a command button. It will insert a row when the value changes in column K starting at row 11 (colors). It works great, but I wonder if there is a way to add in the formulas and formatting to the inserted rows?

Code:
Dim LR As Long, i As Long
LR = Range("K" & Rows.Count).End(xlUp).Row
For i = LR To 12 Step -1
    If Range("K" & i).Value <> Range("K" & i - 1).Value Then Rows(i).Insert
Next i
End Sub

Any help would be greatly appreciate. I truly thank you!
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
How about
Code:
Dim LR As Long, i As Long
LR = Range("K" & Rows.Count).End(xlUp).Row
For i = LR To 12 Step -1
    If Range("K" & i).Value <> Range("K" & i - 1).Value Then
      Rows(i).Insert
      Rows(i - 1).Resize(2).Filldown
   End If
Next i
 
Upvote 0
Do you mean that it's not inserting the new rows?
 
Upvote 0
It is inserting the new rows, but it's also filling them with the contents of the row above.
If that is not what you are after can you please explain what you want.
 
Upvote 0
I do apologize, I don't think I am explaining myself correctly - My original code does insert new rows, but when I added the part you created it didn't add in rows and fill w/ the formatting/formulas only from the other cells (it did copy exactly what was in the above row). I don't want to copy - just want to keep the formatting/formulas. The code I'm using now to insert rows after word change (loops from bottom to top) works, but isn't keeping the borders and formulas in the new rows.
 
Last edited:
Upvote 0
How about
Code:
Sub slivesay()
Dim LR As Long, i As Long
LR = Range("K" & Rows.Count).End(xlUp).Row
For i = LR To 12 Step -1
    If Range("K" & i).Value <> Range("K" & i - 1).Value Then
      Rows(i).Insert
      Rows(i - 1).Resize(2).Filldown
      Rows(i).SpecialCells(xlConstants).ClearContents
   End If
Next i
End Sub
 
Upvote 0
That is wonderful!!! Thank you so much! I am using this code on 9 different sheets. I have a userform to choose items, command button to move items to these 9 sheets and paste items where they go on each sheet and calculate what is needed for each items sizes (all different) - last step is to sort by certain criteria and split by color and add in new line. This is perfect!!!!!! Again, thank you so much! The updated coded works wonderfully!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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