Macro to Conditionally Format

atgordon

Board Regular
Joined
Nov 17, 2004
Messages
118
I was hoping someone could help me construct a short macro. I have a range of cells on a worksheet (B5:Y40). I want the macro to look at each of the cells in that range and if the absolute value is between 0 and 1, it formats the cell as #,##0.00. If the absolute value is greater than 1, it formats the cell as #,##0. I also want to include some sort of IsError statement or comparable that will exit the macro if the cell contains an error of any type. I don't want this macro to automatically calculate, but rather only run when I manually do so.

Any help would be greatly appreciated.

Thanks.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Maybe something like this!!
Code:
Sub Doit()
Dim cl As Range
On Error Resume Next
For Each cl In Range("B5:Y40")
If Abs(cl) < 1 Then
    cl = Format(Abs(cl), "#0.00")
Else: cl = Format(Abs(cl), "#0")
End If
Next cl
End Sub

lenze
 
Upvote 0
Thanks - that is great. One more question - how would I add a condition so that if the abs value = 0, the cell format would be "-". Essentially, it would have 3 conditions - if the abs is less than 0, if it equals 0 and everything else. Thanks.
 
Upvote 0
Also, if possible, I don't want the cell to show the absolute value - for instance, if before the macro, the cell's value is -6, after running the macro, it is (6). I just mentioned absolute value as a way to avoid having to choose between 0 and 1 and between 0 and -1.

Thanks.
 
Upvote 0
Give this a shot
Code:
Sub Doit()
Dim cl As Range
On Error Resume Next
For Each cl In Range("B5:Y40")
If cl = 0 Then
   cl = Format(cl, "-")
ElseIf Abs(cl) < 1 Then
    cl = Format(cl, "#0.00")
Else: cl = Format(cl, "#0")
End If
Next cl
End Sub

lenze
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,916
Members
452,949
Latest member
beartooth91

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