Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 10
Default Formatting a number

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


  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
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



  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 751
Default Formatting a number

I can see only a VBA solution to this problem. I suggest the following
approach:

You use an event procedure for the Change event. In this procedure you
specify the range for which such behavior is desired. If you want you
can set (from Excel) the number format to % beforehand, to facilitate
editing.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim AutoFormatArea
Set AutoFormatArea = Range("A:A") '<<< Change Range specification here
If Intersect(Target, AutoFormatArea) Is Nothing Then Exit Sub
If Target.Value * 1000 Mod 10 = 0 Then
Target.NumberFormat = "0%"
Else
Target.NumberFormat = "0.0%"
End If
End Sub

To install:
1. Right click the sheet tab where you want this behavior. Choose View
Code...
2. The VBA IDE will appear. Paste the above code in the sheet module
window.

HTH
Kostis Vezerides

On Jul 9, 6:29 pm, "Michel Khennafi" wrote:
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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Number formatting pm Excel Discussion (Misc queries) 2 May 30th 07 02:53 PM
Number formatting RMP Excel Discussion (Misc queries) 4 February 24th 07 09:39 PM
number formatting Louise Excel Worksheet Functions 5 November 24th 06 07:16 PM
formatting cell number based on previous cell number Pasquini Excel Discussion (Misc queries) 3 June 20th 06 06:36 AM
Number formatting - what's going on? yhtak Excel Discussion (Misc queries) 3 July 11th 05 10:57 AM


All times are GMT +1. The time now is 11:32 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"