![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: May 2002
Posts: 2
|
In addition to the +;0;-;text set of numeric formats, I understand one can scale the presentation of a number. However, I don't find anything about this in "help". What I want is a way to display a wide range of values such as .01, 2, 3000 as, for example, 10mg, 2g, 3kg. Is this possible?
|
|
|
|
|
|
#2 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
|
Quote:
2 using 0"g" to get 2g 3000 using 0,"kg" to get 3kg ...but, not 0.01 to get 10mg |
|
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
|
Hi Ljvs,
Here is a user-defined function (UDF) that does this: Function FormatGms(Grams As Double) As String Const Fmt = "0" Select Case Grams Case 0 FormatGms = "0g" Case Is > 1000 FormatGms = Format(Grams / 1000, Fmt) & "Kg" Case Is > 1 FormatGms = Format(Grams, Fmt) & "g" Case Is > 0.001 FormatGms = Format(Grams * 1000, Fmt) & "mg" End Select End Function Just put this code in a macro module, and call it like: =FormatGms(A1)
__________________
Keep Excelling. Damon VBAexpert Excel Consulting (My other life: http://damonostrander.com ) |
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
|
Damon, your UDF creates a text value rather than formatting... right?
|
|
|
|
|
|
#5 |
|
New Member
Join Date: May 2002
Posts: 2
|
Many thanks to Mark W. His response got me going. If I store the smallest unit (milligrams in the example), then the following format will usefully compress a very wide range of values:
[<1000] 0" mg";[>=1000000] #,#00,," kg"; 0," g " The extra spaces in " g " keep the units pretty well lined up. |
|
|
|
|
|
#6 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
|
Quote:
If you apply this format to a cell containing 0.01 and expect to get 10 mg... you won't! You'll get 0 mg instead! The problem is that formatting never alters the underlying value; however, in the case of 0.01g to 10mg you need to move the decimal place (by multiplying by 1000). Fomatting won't do this for you. |
|
|
|
|
|
|
#7 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
|
Mark,
Yes, my UDF displays the result as text. Thus one can't use the underlying value for calculation--just for display. But the value in A1 can be use for calculation. Using a UDF like this to display the result as text is definitely not the ideal, but sometimes is the only way to get the desired formatting. Damon |
|
|
|
|
|
#8 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
|
Quote:
|
|
|
|
|
|
|
#9 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
|
Hi again Mark,
It's been a while, but just to show you that I haven't forgotten-- Regarding writing a macro that would use cell formatting, I can't see a way to do that because I know of no way to display the value as milligrams when it is less than one. Unfortunately, Excel formatting only provides a way to scale numbers moving the decimal place to the left, not to the right. So even a macro can't display the number in milligrams without modifying the underlying cell value. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|