Simple Tweak in excel converting Negative to Positive and changing FONT color

Jyotirmaya

Board Regular
Joined
Dec 2, 2015
Messages
205
Office Version
  1. 2019
Platform
  1. Windows
I am using the following code to make all the negative value to positive

Sub test()
For Each Cell In Range("A1:Z10")
If Cell.Value < 0 Then
Cell.Value = Abs(Cell.Value)
End If
Next Cell
End Sub

I want that the converted negative values should be of font color RED.
what should be the tweak for the code ??
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Assuming the range is filled with numerical values, try the following in a copy of your workbook.

Rich (BB code):
Sub ConvertAndColour()
  With Range("A1:Z10")
    .Value = Evaluate(Replace("if(#<0,""a"" & #,#)", "#", .Address))
    .SpecialCells(xlConstants, xlTextValues).Font.Color = vbRed
    .Replace What:="a-", Replacement:="", LookAt:=xlPart
  End With
End Sub
 
Upvote 0
If the range could contain text and/or blank cells as well as numerical values, then try

Rich (BB code):
Sub ConvertAndColour()
  Dim a As Variant
  
  With Range("A1:Z10")
    a = .Value
    .Value = Evaluate(Replace("if(len(@),if(@<0,""#N/A"",@),"""")", "@", .Address))
    .SpecialCells(xlConstants, xlErrors).Font.Color = vbRed
    .Value = a
    .Value = Evaluate(Replace("if(len(@),if(@<0,-@,@),"""")", "@", .Address))
  End With
End Sub
 
Upvote 0
Assuming the range is filled with numerical values, try the following in a copy of your workbook.

Rich (BB code):
Sub ConvertAndColour()
  With Range("A1:Z10")
    .Value = Evaluate(Replace("if(#<0,""a"" & #,#)", "#", .Address))
    .SpecialCells(xlConstants, xlTextValues).Font.Color = vbRed
    .Replace What:="a-", Replacement:="", LookAt:=xlPart
  End With
End Sub


Thank you Peter
 
Upvote 0
That was an awesome solution! I've never seen evaluate used that way. Very slick!
 
Upvote 0
Thank you Peter
You are very welcome.

That was an awesome solution! I've never seen evaluate used that way. Very slick!
Thanks for your kind comments.

On further reflection, if there are no text cells (or you don't mind if they go red), only numbers and/or blanks, there was no need to even use Evaluate. This should do just as well.

Rich (BB code):
Sub ConvertNColour()
   With Range("A1:Z10")
    .Replace What:="-", Replacement:="#-", LookAt:=xlPart
    .SpecialCells(xlConstants, xlTextValues).Font.Color = vbRed
    .Replace What:="#-", Replacement:="", LookAt:=xlPart
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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