Insert a row above unique value in column using VBA

Lensas

New Member
Joined
Aug 14, 2018
Messages
3
Hello experts,

I have a report that I need to format quite often, to make it more readable I need to insert yellow rows above every market. Any suggestions on how to find the first unique value in a column?

To make it more clear: Column Q contains country names and by adding yellow lines I want to distinguish sections for every separate country.

Hope this makes sense. Thanks in advance.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Welcome to Mr Excel :)

Try the below VBA code … To add the code, you need to follow the below steps:


  1. Press Alt+F11
  2. Press Alt+I+M
  3. Paste the below VBA code below
  4. Close the VBA editor & select the sheet you need to add the yellow rows
  5. Press F8 & run the code
  6. Ensure to save the file in macro enable format for the code to work (.xlsm or .xlsb)

Code:
Sub Add_Yellow_Rows()
With ActiveSheet
    For x = .Range("Q" & Rows.Count).End(xlUp).Row To 2 Step -1
        If .Range("Q" & x).Value <> .Range("Q" & x).Offset(-1).Value Then
            With Rows(x)
                .EntireRow.Insert
                .Offset(-1).Interior.Color = vbYellow
            End With
        End If
    Next
End With
End Sub
 
Last edited:
Upvote 0
Thank you mse!

I will try it out and come back if any issues, looks quite simple, but somehow did not manage to crack it myself...
 
Upvote 0
Thanks once again, I studied the code you provided and adjusted it to my needs. Works perfectly, and I finally learned the Step keyword!
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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