Highlight cells with same text

dado6612

Well-known Member
Joined
Dec 4, 2009
Messages
591
Hi to all

I want code which will, on some command (ex. double click) highlight all cells in range if they have same text as selected cell.
Ok, I assume that's easy, but problem is, I have merged cells and by my small experience with VBA, I know that codes doesn't work properly with merged cells.

Any help?

Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
this works for me. Copy this code into the worksheet module:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Application.EnableEvents = False
    HighlightCellsWithSameValue
    Cancel = True
    Application.EnableEvents = True
End Sub

Then copy this into a standard code module:

Code:
Option Explicit

Sub HighlightCellsWithSameValue()
Dim rngUsedRange As Range, cell As Range
Dim x As Variant, intResponse As Long, i As Long
Dim strActSheet As String
Dim rngNewRange As Range, rngMyRange As Range


'   Abort if a range isn't selected
    If TypeName(Selection) <> "Range" Then
        MsgBox "Please select a single cell", vbOKOnly + vbInformation, "Invalid Range Selection"
        Exit Sub
    End If
    
    If Selection.Cells.Count > 1 Then
        MsgBox "Please select a single cell", vbOKOnly + vbInformation, "Invalid Range Selection"
        Exit Sub
    End If
    
    x = ActiveCell.Value
    If x = Empty Then
        intResponse = MsgBox("The current cell is empty. Do you wish to highlight all empty cells in the worksheet?", vbOKCancel, "Highlight Cells with Same Value")
        If intResponse = vbCancel Then
            GoTo endmacro
        End If
    End If
    
    On Error GoTo endmacro
    intResponse = MsgBox("This macro will highlight all cells with " & x & " in the current worksheet", vbOKCancel, "Highlight Cells with Same Value")
    If intResponse = vbOK Then
        Application.ScreenUpdating = False
        strActSheet = ActiveSheet.Name
        
        With Range([A1], [A1].SpecialCells(xlLastCell)).Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        
        Set rngUsedRange = ActiveSheet.UsedRange
        Set rngMyRange = Intersect(ActiveCell.EntireColumn, rngUsedRange)
    '   Define area that matches selected cell value
        i = 0
        For Each cell In rngUsedRange
            If cell.Value = x Then
                If i = 0 Then
                    Set rngNewRange = cell
                Else
                    Set rngNewRange = Union(rngNewRange, cell)
                End If
                i = i + 1
            End If
        Next
        Set rngNewRange = Intersect(rngNewRange, rngUsedRange)
    '    MsgBox rngNewRange.Address
    '   Copy & Paste
        rngNewRange.Interior.ColorIndex = 7
    End If
endmacro:
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Hi dado6612,

I've put together an example WB called "FindAndHighLight.xls" which you can download from:

http://www.box.net/shared/yyuzgkly1r

You'll see there are some cells, including Merged cells with "My Name" in.

Select one of these cells, and Press Ctrl+q.

Regards

ColinKJ
 
Upvote 0
@ btadams:
Thanks for your response, but that code isn't good for me. When code is run, all cells with fill become unfilled and it doesn't work with merged cells. Thanks anyway

@ ColinKJ:
I'll try your way and give u feedback sooner as possible
 
Upvote 0
@ ColinKJ

It's great concept, but with one manna. After one cell is selected and ctrl+q pressed it will highlighted all cell with same value, but, if I want select some new cell and press ctrl+q it will highlight all cells with same value but it wont unhighlight previous cells
 
Last edited:
Upvote 0
Hi dado6612,

OK, replace the code with:

Code:
Sub FindAndHighLight()
A = ActiveCell
If A = "" Then GoTo endd
Cells.Select
Selection.Interior.ColorIndex = xlNone
Range("A1").Select
intRow = ActiveSheet.UsedRange.Rows.Count
intCol = ActiveSheet.UsedRange.Columns.Count
For C = 1 To intCol
For R = 1 To intRow
If Cells(R, C) = A Then
Cells(R, C).Select
Selection.Interior.ColorIndex = 6
End If
Next R
Next C
endd:
End Sub

This clears any previous highlighting before highlighting the new selection.

ColinKJ
 
Upvote 0
Yeah, that's it :biggrin:
I thought it will be harder than 10 lines of code :biggrin:

Thanks
Just 2 more questions :biggrin:

Whats "number" for green color? Edit: Founded, its 4
I need second code which will unhighlight any selection without making new one

Thanks again
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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