marcelita03
New Member
- Joined
- Jan 15, 2013
- Messages
- 38
I usually work on it until I solve it... googling the heck out of it. But it's been days with this one and I can't just figure out what I am doing wrong.
I simply need to change the color cell... if the value of an adjacent cell falls between certain range.
(I CANT USE Conditional Formatting. There is a long reason why. But I don't want to get into that here. I need to do it via Macro)
The cell I need to color has dollars on it. The cell that has the condition is next to it has percent points.
1 Column A Column B
2 $1000 0.001
3 $2000 -3.000
4 $200 0.023
5 $3000 0.000
6 $123 -0.003
If the amount in cell "B2" is less than -1 I want "A2" to be red
If the amount in cell "B2" is between -0.999 and -0.5 I want "A2" to be orange
If the amount in cell "B2" is between -0.499 and 0 I want "A2" to be yellow
If the amount in cell "B2" is between 0.001 and 0.5 I want "A2" to be no color
If the amount in cell "B2" is between 0.499 and 1 I want "A2" to be green
and finally
If the amount in cell "B2" is greater than 1 I want "A2" to be dark green
I need a VBA macro that loops on all values on my list, look for those 6 conditions, and color each cell in A a color depending on what is in column B
This is what I have so far ...
Is not working. The macro runs and colors every cell in my r1 range BLACK! yes BLACK I don't get it.
Any help will be appreciated GREATLY
I simply need to change the color cell... if the value of an adjacent cell falls between certain range.
(I CANT USE Conditional Formatting. There is a long reason why. But I don't want to get into that here. I need to do it via Macro)
The cell I need to color has dollars on it. The cell that has the condition is next to it has percent points.
1 Column A Column B
2 $1000 0.001
3 $2000 -3.000
4 $200 0.023
5 $3000 0.000
6 $123 -0.003
If the amount in cell "B2" is less than -1 I want "A2" to be red
If the amount in cell "B2" is between -0.999 and -0.5 I want "A2" to be orange
If the amount in cell "B2" is between -0.499 and 0 I want "A2" to be yellow
If the amount in cell "B2" is between 0.001 and 0.5 I want "A2" to be no color
If the amount in cell "B2" is between 0.499 and 1 I want "A2" to be green
and finally
If the amount in cell "B2" is greater than 1 I want "A2" to be dark green
I need a VBA macro that loops on all values on my list, look for those 6 conditions, and color each cell in A a color depending on what is in column B
This is what I have so far ...
Code:
Sub color_cells() Dim i As Long, r1 As Range, r2 As Range
Set r1 = Range("A1:A10")
Set r2 = Range("B1:B10")
For Each cell In r1
If IsEmpty(cell) Then GoTo nextcell:
If cell.Value < -1 Then r2.Interior.color = vbRed Else
If cell.Value >= -0.999 And cell.Value < -0.5 Then r2.Interior.color = vbOrange Else
If cell.Value >= -0.499And cell.Value < 0 Then r2.Interior.color = vbYellow Else
If cell.Value >= 0.001 And cell.Value < 0.5 Then r2.Interior.color = vbxlnone Else
If cell.Value >= 0.499 And cell.Value < 1 Then r2.Interior.color = vblightgreen Else
If cell.Value >= 0.1 And cell.Value < 0.25 Then r2.Interior.color = vbGreen Else
If cell.Value > 1Then r2.Interior.color = vbdarkgreen
nextcell:
Next cell
End Sub
Is not working. The macro runs and colors every cell in my r1 range BLACK! yes BLACK I don't get it.
Any help will be appreciated GREATLY
Last edited: