VBA: If cell is highlighted, highlight adjacent cell in next column

patriciajlim

New Member
Joined
Jul 24, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I am trying to write a script that will highlight the cell adjacent to the highlighted cell. Here is what I have come up with but throws an error:

VBA Code:
Sub if_highlight()

Dim c As Range
For Each c In Columns("B")
  If c.Interior.Color = Excel.XlRgbColor.rgbYellow Then
    c.Offset(, 1).Interior.Color = Excel.XlRgbColor.rgbYellow
  End If
Next c

End Sub

The use case is we have first and last names in two columns and we need both the first and last name cells to be highlighted.

Thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi and welcome to MrExcel

Try this
VBA Code:
Sub Macro4()
  Dim lr As Long
  Application.ScreenUpdating = False
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  lr = Range("B" & Rows.Count).End(3).Row
  ActiveSheet.Range("B1:B" & lr).AutoFilter Field:=1, Criteria1:=RGB(255, 255, 0), Operator:=xlFilterCellColor
  lr = Range("B" & Rows.Count).End(3).Row
  If lr > 1 Then Range("C2:C" & lr).Interior.Color = vbYellow
  ActiveSheet.ShowAllData
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,239
Members
448,555
Latest member
RobertJones1986

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