VBA code to make negative numbers red and positive numbers black

commonproblems

New Member
Joined
Apr 25, 2018
Messages
7
Hi all,

I'm trying to write code that allows me to make negative numbers red and positive numbers black for a highlighted range of cells.

it seems to work based on the first cell, so if the first cell is negative all numbers go red and if the first number is positive all numbers go black.
I've pasted the code below, if someone would be able to suggest a fix. Thank you


Sub Decimalworkswith2decimalplaces()
'
' Decimalworkswith2decimalplaces Macro
' decimal works with 2 decimal places
'
Selection.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* """"-""""??_);_(@_)"
If ActiveCell.Value < 0 Then

With Selection.Font
.Color = 255
End With

End If

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
So you want to select a range and then run the script and have these results. Is that what you want.

If not what is the range you want?
 
Upvote 0
Code:
Sub One_Way()
Dim c As Range
    For Each c In Selection
        If c.Value < 0 Then c.Font.Color = vbRed
    Next c
End Sub
It assumes that your regular font color is black. In other words, it does not change the values equal to or more than zero.
 
Upvote 0
why do it with vba when you can do it simply by formatting the cells , select the cells select format number and expand the box and then select negative numbers as RED
 
Upvote 0
Looks like you already have other answers now.
But this was mine.

Code:
Sub Color_My_Selection()
'Modified 11/8/2018 6:17:45 PM  EST
Application.ScreenUpdating = False
Dim r As Range
For Each r In Selection
    If r.Value < 0 Then r.Font.Color = 255
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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