I’m stumped. I’ve written a macro to apply a numeric format based on a cell’s value. In the attached file, the value entered in cell D3 is used to calculate the range D6:D19 & F6:F19. Formatting should be applied as follows:
Values less than 1 & values with decimal places – 0.0#
Whole positive numbers – 0, no decimal places.
The macro is working fine except in cell F19 when some multiples of 12 are entered in D3. For example when D3 is 12 or 24, F19 shows a decimal place, which it shouldn’t. However, when D3 is 36 no decimal place appears, which is correct. All 3 values return a whole positive # with no decimal places. It doesn’t appear to be a rounding error & I believe my macro logic to be correct, but I can’t see where the problem is. Any help would be much appreciated. thanks,
P.S. the code is :
Range(“f17:f19”).Select
Dim MyCell
For Each MyCell In Selection
If MyCell.Value < 1 Then ' #'s less than 1
MyCell.NumberFormat = "0.0#"
ElseIf MyCell.Value / Int(MyCell) = 1 Then 'whole #'s, no decimals
MyCell.NumberFormat = "0"
Else
MyCell.NumberFormat = "0.0#" ' whole #'s with decimals
End If
Next