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

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,526
Office Version
  1. 365
Platform
  1. 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
56,526
Office Version
  1. 365
Platform
  1. Windows
Do you mean that it's not inserting the new rows?
 

slivesay

Board Regular
Joined
Jan 4, 2019
Messages
64

ADVERTISEMENT

Yes, when I tried your code it didn't insert the new rows.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,526
Office Version
  1. 365
Platform
  1. 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

Board Regular
Joined
Jan 4, 2019
Messages
64

ADVERTISEMENT

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
56,526
Office Version
  1. 365
Platform
  1. 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

Board Regular
Joined
Jan 4, 2019
Messages
64
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
56,526
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,129,479
Messages
5,636,575
Members
416,925
Latest member
malamutus

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