How to Insert Blank Row with Shaded Background When Value in Column A Changes, While Ignoring Other Blank Rows

kokoanutt

New Member
Joined
May 17, 2024
Messages
20
Office Version
  1. 365
Platform
  1. Windows
I already have a Macro to insert blank rows when the value in columns C and G change, but now I want to insert a blank row with a shaded background when the value in Column A changes while ignoring other blank rows. Please help!

VBA Code:
Sub BLANKS_SUMMARY2()

    Dim ws As Worksheet
    Dim lr As Long
    Dim i As Long
   
    Set ws = Worksheets("Logged Units Report")
    lr = ws.Range("C" & Rows.Count).End(xlUp).Row
    For i = lr - 1 To 2 Step -1
        If ws.Range("C" & i).Value <> ws.Range("C" & i + 1).Value _
            Or ws.Range("G" & i).Value <> ws.Range("G" & i + 1).Value Then
                ws.Range("C" & i + 1).EntireRow.Insert
        End If
    Next i

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
To clarify: you already ran the macro that inserted blank rows based on C and G, and you want to run another macro to insert blank rows based on row A?

You can do something like this (replace 65535 with whatever color you want):
VBA Code:
Sub BLANKS_SUMMARY3()

    Dim ws As Worksheet
    Dim lr As Long
    Dim i As Long
 
    Set ws = Worksheets("Logged Units Report")
    lr = ws.Range("C" & Rows.Count).End(xlUp).Row
    For i = lr - 1 To 2 Step -1
      If Len(ws.Range("A" & i) > 0 And Len(wsh.Range("A" & i + 1) > 0 _
            And ws.Range("A" & i).Value <> ws.Range("A" & i + 1).Value Then
         ws.Range("A" & i + 1).EntireRow.Insert
         ws.Range("A" & i + 1).EntireRow.Interior.Color = 65535
      End If
    Next i

End Sub

If you are doing this regularly or repeatedly (for C G and now also A), I would incorporate this into the original code.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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