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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
This may put you on the right path:

Excel 2010
ABC
NamesGroupStatus
JENNY HARVEYBlue
CAROL LYNCHMagenta
EILEEN THOMASBlueAvailable
VICTOR DAVISGreen
BOBBY KELLYBlueAvailable
RUTH MORENOYellow
JIM HUNTYellowNot available
GAIL WILSONBlack
JANE VASQUEZRedNot avail.
MELISSA CASTILLOBlack
AGNES DUNCANCyan
JOSHUA HAWKINSYellowavailable.
LYNN AGUILARCyan
SHANNON JONESBlue
STEVEN FIELDSMagenta

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: right"][/TD]

[TD="align: center"]3[/TD]

[TD="align: right"][/TD]

[TD="align: center"]4[/TD]

[TD="align: center"]5[/TD]

[TD="align: right"][/TD]

[TD="align: center"]6[/TD]

[TD="align: center"]7[/TD]

[TD="align: right"][/TD]

[TD="align: center"]8[/TD]

[TD="align: center"]9[/TD]

[TD="align: right"][/TD]

[TD="align: center"]10[/TD]

[TD="align: center"]11[/TD]

[TD="align: right"][/TD]

[TD="align: center"]12[/TD]

[TD="align: right"][/TD]

[TD="align: center"]13[/TD]

[TD="align: center"]14[/TD]

[TD="align: right"][/TD]

[TD="align: center"]15[/TD]

[TD="align: right"][/TD]

[TD="align: center"]16[/TD]

[TD="align: right"][/TD]

</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,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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