VBA Conditional formatting help

x_swinson_x

New Member
Joined
Feb 10, 2016
Messages
18
This is the code that I have for conditional formatting. What I want to happen is as follows

Cells that contain numbers between 0 and 1 to be red
Cells that contain numbers greater than 1 and less than 100 to be yellow
Cells that contain numbers equal to 100 to be green

Here's the current code I'm working with:

Code:
Dim d As DoubleDim r As Range
Set r = ActiveSheet.Range("B15:AF38")
For Each Cell In r
If Cell.Text <> "" And IsNumeric(Cell.Value) = True Then
If Cell.Value > 0 And Cell.Value < 1 Then
Cell.Interior.Color = RGB(255, 0, 0)
If Cell.Value > 100 Then
Cell.Interior.Color = RGB(0, 255, 0)
If Cell.Value > 1 And Cell.Value < 100 Then
Cell.Interior.Color = RGB(255, 255, 0)
End If
End If
End If
End If


Next

Currently the code isn't changing any of the cell backgrounds. Any advice?

Excel 2016, Windows 7
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
This

Code:
Dim d As DoubleDim r As Range

should surely be this

Code:
Dim d As Double
Dim r As Range

???
 
Upvote 0
You need

If... Then
Else

Your Yellow and Green conditions are dependent on your Red condition
 
Upvote 0
This works

Code:
Sub k1()
Dim d As Double
Dim r As Range
Set r = ActiveSheet.Range("A1:A10")
For Each Cell In r
If Cell.Text <> "" And IsNumeric(Cell.Value) = True Then

    If Cell.Value > 0 And Cell.Value < 1 Then
    Cell.Interior.Color = RGB(255, 0, 0)
    End If
    If Cell.Value > 100 Then
    Cell.Interior.Color = RGB(0, 255, 0)
    End If
    If Cell.Value > 1 And Cell.Value < 100 Then
    Cell.Interior.Color = RGB(255, 255, 0)
    End If
End If
Next
End Sub
 
Upvote 0
This works

Code:
Sub k1()
Dim d As Double
Dim r As Range
Set r = ActiveSheet.Range("A1:A10")
For Each Cell In r
If Cell.Text <> "" And IsNumeric(Cell.Value) = True Then

    If Cell.Value > 0 And Cell.Value < 1 Then
    Cell.Interior.Color = RGB(255, 0, 0)
    End If
    If Cell.Value > 100 Then
    Cell.Interior.Color = RGB(0, 255, 0)
    End If
    If Cell.Value > 1 And Cell.Value < 100 Then
    Cell.Interior.Color = RGB(255, 255, 0)
    End If
End If
Next
End Sub

This worked perfectly thanks so much!
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,573
Members
449,089
Latest member
Motoracer88

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