Highlight all the occurances when we click or highlight one cell

expert40

Board Regular
Joined
Feb 12, 2012
Messages
71
Hi ,

I have one sheet as follows


x4567 x4567
x4568 x4568
x4569 x4569
x4570 x4567 x4570
x4571 x4568 x4571
x4572 x4569 x4572
x4573 x4570 x4573
x4574 x4571 x4574
x4575 x4572 x4575
x4576 x4573 x4576
x4577 x4574 x4577
x4575
x4576
x4577


So when i click on x4567 in first cell ,I should simple highlight all x4567.

When i deselect x4567 highlight should be removed.

Please help me how to proceed
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Put this code in Worksheet Module by Right Clicking on Sheet's Tab and then choosing "View Code". Paste this code. This should get you "started"
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static sOldRng As Range
Dim r As Range

If Target.Cells.Count > 1 Then Exit Sub
If Target.Value = "" Then Exit Sub

If Not sOldRng Is Nothing Then
    With sOldRng
        .Font.ColorIndex = xlColorIndexAutomatic
        .Interior.ColorIndex = xlColorIndexNone
    End With
    Set sOldRng = Nothing
End If

For Each r In ActiveSheet.UsedRange
    If r.Value = Target.Value Then
        If sOldRng Is Nothing Then
        Set sOldRng = r
        Else
        Set sOldRng = Union(sOldRng, r)
        End If
    End If
Next r

With sOldRng
    .Font.Color = vbRed
    .Interior.Color = vbYellow
End With

End Sub
 
Upvote 0
another way, with conditional formatting:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count = 1 Then
With UsedRange
  .FormatConditions.Delete
  .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=" & Target.Address
  .FormatConditions(1).Interior.ColorIndex = 7
End With
End If
End Sub
This can be restricted to a portion of the sheet.
 
Upvote 0
This is perfect.I ws really helpful for me.you t the first time itself catched my problem and solved it.
 
Upvote 0
Put this code in Worksheet Module by Right Clicking on Sheet's Tab and then choosing "View Code". Paste this code. This should get you "started"
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static sOldRng As Range
Dim r As Range

If Target.Cells.Count > 1 Then Exit Sub
If Target.Value = "" Then Exit Sub

If Not sOldRng Is Nothing Then
    With sOldRng
        .Font.ColorIndex = xlColorIndexAutomatic
        .Interior.ColorIndex = xlColorIndexNone
    End With
    Set sOldRng = Nothing
End If

For Each r In ActiveSheet.UsedRange
    If r.Value = Target.Value Then
        If sOldRng Is Nothing Then
        Set sOldRng = r
        Else
        Set sOldRng = Union(sOldRng, r)
        End If
    End If
Next r

With sOldRng
    .Font.Color = vbRed
    .Interior.Color = vbYellow
End With

End Sub


Thanks a lot dude .Its a great help for me.It worked as perfect
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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