Applying style based on value

gigi79

New Member
Joined
Sep 18, 2012
Messages
34
Hi all,
Does anyone know of a way to apply a style to cells based on their value/type?

I have a table that I send out and each data type has a different style. I’m currently going through and applying the style to the cells as required. M wondering if there is a way to do this without me having to do so manually.

there are three styles in use: one for date values, one for state values and one for name values.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try something like the code below. You can change the style names to suit yours.

Assumptions
1. Dates are entered as actual dates and are not strings
2. States are two letter abbreviations
3. Names are anything else

If the assumptions above are not correct, please post a copy of your data with the XL2BB tool.

VBA Code:
Sub ApplyStyles()
    Dim r As Range, c As Range
    
    Set r = Application.InputBox("Select range to apply styles.", "Styles", , , , , , 8)
    
    If Not r Is Nothing Then
        For Each c In r
            If Len(c) > 2 And Not (IsDate(c)) Then c.Style = "Neutral"
            If IsDate(c) Then c.Style = "Good"
            If Len(c) = 2 Then c.Style = "Bad"
        Next c
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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