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

slivesay

New Member
Joined
Jan 4, 2019
Messages
44
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:

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,882
Office Version
365
Platform
Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,882
Office Version
365
Platform
Windows
Do you mean that it's not inserting the new rows?
 

slivesay

New Member
Joined
Jan 4, 2019
Messages
44
Yes, when I tried your code it didn't insert the new rows.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,882
Office Version
365
Platform
Windows
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.
 

slivesay

New Member
Joined
Jan 4, 2019
Messages
44
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:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,882
Office Version
365
Platform
Windows
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
 

slivesay

New Member
Joined
Jan 4, 2019
Messages
44
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:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,882
Office Version
365
Platform
Windows
You're welcome & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,095,984
Messages
5,447,700
Members
405,463
Latest member
Tommy5

This Week's Hot Topics

Top