Highlight Cell with Certain Value (VBA)

gripper

Board Regular
Joined
Oct 29, 2002
Messages
176
I have a workbook and what I have been using to highlight cells is Conditional Formatting. The issue I have now is when I copy this to another sheet with other data the conditional formatting is conflicting with other items on that new sheet so I need to get away from that and just use highlighting in its basic form.

What I want to do is the following:

Column X through Column AF any cell within these columns with a value of greater than zero I want to highlight orange.
This will be from Row 2 to last Row in Each column. (Each column has the same number of rows).

In my macro I currently have a last row variable as "lrow = Cells(Rows.Count, "B").End(xlUp).Row"

I thank you in advance for your assistance.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Code below
- loops all cells in the active sheet in columns X to AF, rows 2 to lRow
- identifies relevant cells and puts them in one range (using UNION function)
- clears previous interior cell formatting in X to AF
- colours relevant cells

VBA Code:
Sub Highlight()
    Dim lRow As Long, Cel As Range, Data As Range, Orange As Range
    With ActiveSheet
        lRow = .Cells(.Rows.Count, "B").End(xlUp).Row
        Set Data = .Range("X2:AF" & lRow)
        For Each Cel In Data
            If IsNumeric(Cel) Then
                If Cel > 0 Then
                    If Not Orange Is Nothing Then Set Orange = Union(Orange, Cel) Else Set Orange = Cel
                End If
            End If
        Next Cel
        Data.Interior.Color = xlNone
        If Not Orange Is Nothing Then Orange.Interior.Color = 49407
    End With
End Sub
 
Upvote 0
Solution
Yongle,

Thank you so much for taking the time to post a reply. This code works perfectly.

Again, Thank you
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,759
Members
449,048
Latest member
excelknuckles

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