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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,212,056
Messages
6,105,650
Members
447,974
Latest member
misspancake

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