Insert row where cells in column are different but ignore blanks

somesoldiers

Board Regular
Joined
Feb 23, 2008
Messages
199
Hi Guys

Can you advise how I can amend the below code so it will not add any rows where a cell in column F is blank please?

Code:
Sub InsRow()
Dim LastRow As Long
Dim i As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
For i = LastRow To 2 Step -1
If .Cells(i, "F").Value <> .Cells(i - 1, "F").Value Then
.Rows(i).Insert
End If
Next
End With
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
or better still amend to "if column F is different OR column G is different" insert a new row- this will save running the macro twice and just changing the column
 
Upvote 0
bump- thanks
Code:
Sub InsRow()
Dim LastRow As Long
Dim i As Long
Dim a As Range 'just to save writing .cells a number of times
With ActiveSheet
Set a = .Cells
LastRow = a(.Rows.Count, "F").End(xlUp).Row
For i = LastRow To 2 Step -1
If (a(i, "F").Value <> a(i - 1, "F").Value) _
    * (a(i, "F") <> "") * (a(i - 1, "F") <> "") Then .Rows(i).Insert
Next
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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