View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Bernie Deitrick Bernie Deitrick is offline
external usenet poster
 
Posts: 5,441
Default Formatting a number

Michel,

There is no formatting that will do that. You would need to use the worksheet change or worksheet
calculate event - the choice would depend on how your sheet is structured.

Copy the code below, right-click the sheet tab, select "View Code" and past e the code into the
window that appears.

As written, this will apply the format to cells in the range B1:B10.

HTH,
Bernie
MS Excel MVP

Private Sub Worksheet_Calculate()
Dim myCell As Range
For Each myCell In Range("B1:B10")
If Int(myCell.Value * 100) = myCell.Value * 100 Then
myCell.NumberFormat = "0%"
Else
myCell.NumberFormat = "0.0%"
End If
Next myCell
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Dim myCell As Range
For Each myCell In Range("B1:B10")
If Int(myCell.Value * 100) = myCell.Value * 100 Then
myCell.NumberFormat = "0%"
Else
myCell.NumberFormat = "0.0%"
End If
Next myCell
End Sub



"Michel Khennafi" wrote in message
...
Good morning...

I have a situation where I would like the formatting of a % number to follow the rules:
- if the number has no decimal then print the number as is for instance 98%
- if the number has more than one decimals, print the number with only one decimal, for instance
98.17% becomes 98.2%

One of the goals is to avoid such displays as 98.00%

I tried to play with the format but never had a chance to figure it out... has anyone handled this
situation before? should I create a custom format?

Thanks so much for your assistance

Michel