Toggle highlighting/unhighlighting cells

phonesdontfly

New Member
Joined
Mar 2, 2016
Messages
16
Hi!

I would like to have a macro that I can "toggle" on and off that will highlight all cells that have a user-inputted word.

For instance, if the user inputs Available", then I would like all the cells that contain the word "Available" to highlight yellow. If I run the macro again, I would like them to unhighlight.

I've tried conditional formatting, but that doesn't support the toggle function- it just recreates the same rule over and over with each run of the macro.

Thanks!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
This may put you on the right path:

Excel 2010
ABC
1NamesGroupStatus
2JENNY HARVEYBlue
3CAROL LYNCHMagenta
4EILEEN THOMASBlueAvailable
5VICTOR DAVISGreen
6BOBBY KELLYBlueAvailable
7RUTH MORENOYellow
8JIM HUNTYellowNot available
9GAIL WILSONBlack
10JANE VASQUEZRedNot avail.
11MELISSA CASTILLOBlack
12AGNES DUNCANCyan
13JOSHUA HAWKINSYellowavailable.
14LYNN AGUILARCyan
15SHANNON JONESBlue
16STEVEN FIELDSMagenta

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



Code:
Sub ToggleStatus()Dim sStatus As String
Dim rng As Range
Dim cel As Range


sStatus = InputBox(Prompt:="Criteria:", Title:="Highlight Matches", _
        Default:="Available")


Set rng = Range("C2:C" & Cells(Rows.Count, 3).End(xlUp).Row)
For Each cel In rng
    Debug.Print cel.Address
    If InStr(1, cel.Value, sStatus, vbTextCompare) > 0 Then
        With cel.Interior
            If .ColorIndex = 6 Then
                .ColorIndex = -4142
            Else
                .ColorIndex = 6
            End If
        End With
    End If
Next cel


End Sub

The above code should toggle the highlight for any cells in column C matching the search criteria. As is, it is not case sensitive and will look at the contents of each cell to find a match. If you search avail, it will toggle 4,6,8,10,13.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,303
Members
449,218
Latest member
Excel Master

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