
Tips.Net > ExcelTips Home > Macros > Engineering Calculations
Summary: In an engineering or scientific environment, it is not unusual to need data "normalized" in some manner. This tip examines a couple of different ways you can normalize your data, including a method that adds units and the appropriate prefixes for those units. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
In an engineering environment, it is not unusual to need to "normalize" numbers in some manner. For instance, you may need to show numeric values normalized to multiples of 10^3, such that 7340 is expressed as 7.34 and 73400 is expressed as 73.4.
It is possible in Excel to use a custom number format to express information in scientific notation that will normalize the display of a number to a multiple of 10^3. To do this, you would follow these steps:
Now, when you enter a number such as 7340 into the cell, Excel displays it as 7.3E+3. Because of the way the cell format was entered, the portion after the E will always be a multiple of 3.
This is fine and good, but what if you want just the 7.3 in the cell, and then a metric prefix with a unit in an adjoining cell, such as kilograms? This is a bit more complex, but it can be done using formulas. For instance, let's assume you have your original number in cell A2, you wanted the normalized number in cell B2, and the metic prefix and unit name in cell C2. All you would need to do is enter the following formula in cell B2:
=IF(OR(A2>=1,A2<=-1),SIGN(A2)*(ABS(A2)/(10^(3*INT(LOG(ABS(A2))/3)))), IF(A2=0,0,SIGN(A2)*(ABS(A2)*10^(-3*INT(LOG(ABS(A2))/3)))))
Assuming the units you are working with are an imaginary unit called a foo, in cell C2 you would use a different formula, as follows:
=IF(OR(A2>=1, A2<=-1),CHOOSE(INT(LOG(ABS(A2))/3)+1, "Foos", "Kilofoos", "Megafoos", "Gigafoos", "Terafoos", "Petafoos", "Exafoos"), IF(A2=0,"",CHOOSE(INT(-LOG(ABS(A2))/3)+1, "Millifoos", "Microfoos", "Nanofoos", "Picofoos", "Femtofoos", "Attofoos")))
These formulas may seem a bit long, and they are. However, they will work for any number between approximately -9.99999E-18 to 9.99999E+20. For instance, if you put the number .000125 in cell A2, then cell B2 will contain 125 and cell C2 would contain Millifoos.
If you prefer to not use longer formulas such as these in your workbooks, you can develop a couple of VBA functions to do the trick. The following function, MySciNum, returns a normalized number. Thus, you would use =MySciNum(A2) in cell B2 to get the same results as noted above:
Function MySciNum(BaseNum As Double) As Double
Select Case BaseNum
Case Is >= 1
While Abs(BaseNum) > 1000
BaseNum = BaseNum / 1000
Wend
Case 0
'Do nothing
Case Else
While Abs(BaseNum) < 1
BaseNum = BaseNum * 1000
Wend
End Select
MySciNum = BaseNum
End Function
This function only returns a number. To return the units with the appropriate metric prefix, you would use the following function. All you need to do is pass it the cell reference and the name of a single unit. For instance, you could use =MySciPre(A2, "foo"). The macro is as follows:
Function MySciPre(BaseNum As Double, Unit As String) As String
Dim OrigNum As Double
Dim Pref As Integer
Dim Temp As String
Pref = 0
OrigNum = BaseNum
Select Case BaseNum
Case Is >= 1
While Abs(BaseNum) > 1000
BaseNum = BaseNum / 1000
Pref = Pref + 1
Wend
Case 0
Pref = 99
Case Else
While Abs(BaseNum) < 1
BaseNum = BaseNum * 1000
Pref = Pref - 1
Wend
End Select
Select Case Pref
Case -6
Temp = "atto" & Unit
Case -5
Temp = "femto" & Unit
Case -4
Temp = "pico" & Unit
Case -3
Temp = "nano" & Unit
Case -2
Temp = "micro" & Unit
Case -1
Temp = "milli" & Unit
Case 0
Temp = Unit
Case 1
Temp = "kilo" & Unit
Case 2
Temp = "mega" & Unit
Case 3
Temp = "giga" & Unit
Case 4
Temp = "tera" & Unit
Case 5
Temp = "peta" & Unit
Case 6
Temp = "exa" & Unit
Case Else
Temp = ""
End Select
If Len(Temp) > 0 Then
Temp = LCase(Temp)
Temp = UCase(Left(Temp, 1)) & Mid(Temp, 2)
If Abs(OrigNum) <> 1 Then Temp = Temp & "s"
End If
MySciPre = Temp
End Function
Tip #2928 applies to Microsoft Excel versions: 97 2000 2002 2003
Don't Go in Debt for Christmas! Tired of trying to keep up with the Joneses for Christmas? Want to enjoy the season rather than dread the aftermath? Learn how you can avoid the financial traps that spring up every Christmas.
Check out Top Fifteen Tips for Financing Christmas today!
Add power to your purpose with Excel. A comprehensive 500+ page e-book explains everything you need to know about macros. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site