simple macro to format numbers doesnt work

helpsky

New Member
Joined
May 13, 2018
Messages
7
<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}</style>i have hte below code but it doesnt work for some reason! the code runs without breaking but it does not format at all. after running the code, if you look at the conditional formatting button in the ribbon, it shows that the formatting is there but i just doesnt apply it to any cells. all i want if for number greater than one to have 1 decimal place and number less than or equal to one to have two decimal places. please advise!!! thank you!!!!


Code:
Sub recent()
   
    sorter_col = Cells.Find("Spread", [A1], , , xlByColumns, xlPrevious).Column
  
    Dim rg As Range
    Dim cond1 As FormatCondition, cond2 As FormatCondition
    Set rg = Cells.Columns(sorter_col)
    Cells.Columns(sorter_col).Select
   
    
    'clear any existing conditional formatting
    rg.FormatConditions.Delete
 
    'define the rule for each conditional format
    Set cond1 = rg.FormatConditions.Add(xlCellValue, xlGreater, "=1")
    Set cond2 = rg.FormatConditions.Add(xlCellValue, xlLessEqual, "=1")
 
    'define the format applied for each conditional format
    With cond1
    NumberFormat = "0.0"
    End With
 
    With cond2
    NumberFormat = "0.00"
    End With
   
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi helpsky

I'm not aware of conditional formatting changing number formatting of cells. For that you need the 'Format Cells' function (right click on a cell -> 'Format Cells' or SHIFT + F10 -> 'Format Cells').

The following Worksheet_Change event (place the code in the worksheet's code sheet) operates on cell A1 shows this in action:

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

If Target = Range("A1") Then
    If Target.Value2 > 1 Then
    
        Target.NumberFormat = "0.0"
        
    Else
    
        Target.NumberFormat = "0.00"
        
    End If
    
End If

End Sub

Cheers

pvr928
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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