MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 13th, 2002, 07:55 AM   #1
Ljvs
New Member
 
Join Date: May 2002
Posts: 2
Default

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?
Ljvs is offline   Reply With Quote
Old May 13th, 2002, 08:31 AM   #2
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

Quote:
On 2002-05-13 06:55, Ljvs wrote:
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?
Using built-in formatting you can display...

2 using 0"g" to get 2g
3000 using 0,"kg" to get 3kg

...but, not 0.01 to get 10mg
Mark W. is offline   Reply With Quote
Old May 13th, 2002, 09:05 AM   #3
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

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 )
Damon Ostrander is offline   Reply With Quote
Old May 13th, 2002, 09:10 AM   #4
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

Damon, your UDF creates a text value rather than formatting... right?
Mark W. is offline   Reply With Quote
Old May 13th, 2002, 12:33 PM   #5
Ljvs
New Member
 
Join Date: May 2002
Posts: 2
Default

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.
Ljvs is offline   Reply With Quote
Old May 13th, 2002, 12:41 PM   #6
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

Quote:
On 2002-05-13 11:33, Ljvs wrote:
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.
Hmmmm... I'd be careful there...

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.
Mark W. is offline   Reply With Quote
Old May 13th, 2002, 04:14 PM   #7
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

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
Damon Ostrander is offline   Reply With Quote
Old May 13th, 2002, 04:43 PM   #8
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

Quote:
On 2002-05-13 15:14, Damon Ostrander wrote:
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
The reason why I asked... Why couldn't a macro change the value's scale and then apply the appropriate custom format. For example, change 0.01 to 10 and format as 0"mg".
Mark W. is offline   Reply With Quote
Old May 17th, 2002, 04:03 PM   #9
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

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.
Damon Ostrander is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 05:50 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes