Excel - How to format a number as B, KB, MB, or GB?
Asked By Square Peg
11-Jan-09 11:32 PM
Is there a way to format the value in a cell so that
if it's it displays as
without changing the actual value in the cell?
Microsoft Excel
(1)
Intersect
(1)
Target.NumberFormat
(1)
Worksheet
(1)
TEXT
(1)
Selection.Formula
(1)
Target.Value
(1)
NnnGB
(1)
Héctor Miguel replied...
hi, !
I left-out the first section (nnnB), for the other three I used this custom format:
[<1e6]0.0," Kb";[<1e9]0.0,," Mb";0.0,,," Gb"
change the order if you use more "B" than "Gb"
hth,
hector.
Bernard Liengme replied...
Alternatively, use Engineering Notation - scientific notation with powers of
3
http://people.stfx.ca/bliengme/ExcelTips/EngineeringNotation.htm
best wishes
--
Bernard V Liengme
Microsoft Excel MVP
http://people.stfx.ca/bliengme
remove caps from email
Ajay replied...

You can't do that with straight cell formatting, but you can implement
it
using event code. Right click the tab at the bottom of the worksheet
and
select View Code from the popup menu that appears, then copy/paste
the
following into the code window that opened up...
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C:C")) Is Nothing Then
If Target.Value < 1000 Then
Target.NumberFormat =3D "0 \B"
ElseIf Target.Value < 999500 Then
Target.NumberFormat =3D "0.000, \K\B"
ElseIf Target.Value < 999500000 Then
Target.NumberFormat =3D "0.000,, \M\B"
ElseIf Target.Value < 999500000000# Then
Target.NumberFormat =3D "0.000,,, \G\B"
Else
Target.NumberFormat =3D "0.000,,,, \T\B"
End If
End If
End Sub
Since you didn't tell us what cells, I assumed Column "C" (change the
Range
inside the Intersect function to the cell range you want to have this
functionality). After doing this, any numbers entered into those cells
will
adopt the number format you requested. Note that existing numbers will
not
change unless re-entered. You can do that one at a time or you can
select
all the existing numbers and execute this code from the Immediate
Window...
angelo replied...
Hi there,
well it is n
Square Peg a écrit :
Rick Rothstein replied...

When you copied that entire text (verbatim, by the way) from when I
originally posted that solution on December 14, 2008 (subject line
sentence...
numbers and execute this code from the Immediate Window..."
doesn't mean much unless you include the last line I originally posted,
namely, this line...
Selection.Formula = Selection.Formula
--
Rick (MVP - Excel)
You can't do that with straight cell formatting, but you can implement
it
using event code. Right click the tab at the bottom of the worksheet
and
select View Code from the popup menu that appears, then copy/paste
the
following into the code window that opened up...
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C:C")) Is Nothing Then
If Target.Value < 1000 Then
Target.NumberFormat = "0 \B"
ElseIf Target.Value < 999500 Then
Target.NumberFormat = "0.000, \K\B"
ElseIf Target.Value < 999500000 Then
Target.NumberFormat = "0.000,, \M\B"
ElseIf Target.Value < 999500000000# Then
Target.NumberFormat = "0.000,,, \G\B"
Else
Target.NumberFormat = "0.000,,,, \T\B"
End If
End If
End Sub
Since you didn't tell us what cells, I assumed Column "C" (change the
Range
inside the Intersect function to the cell range you want to have this
functionality). After doing this, any numbers entered into those cells
will
adopt the number format you requested. Note that existing numbers will
not
change unless re-entered. You can do that one at a time or you can
select
all the existing numbers and execute this code from the Immediate
Window...
Square Peg replied...
Oooppps... Sorry. I forgot I already asked and was answered. I think I
got busy with the holidays and forgot to implement your solution. I will
give it a try now.
Ajay replied...
l
ng
Use this formula and your problem will be solved : =3DIF
(A2>999499999,TEXT((A2/999500000),"0.00")&" GB",IF(A2>999499,TEXT
((A2/999500),"00.00")&" MB",IF(A2>999,TEXT((A2/1000),"00.00")
&"KB",A2&" B")))
Rick Rothstein replied...
The OP asked how to "format the value in a cell so that if it's it displays
as...", not how to show it in that format in a different cell.
--
Rick (MVP - Excel)
Use this formula and your problem will be solved : =IF
(A2>999499999,TEXT((A2/999500000),"0.00")&" GB",IF(A2>999499,TEXT
((A2/999500),"00.00")&" MB",IF(A2>999,TEXT((A2/1000),"00.00")
&"KB",A2&" B")))
How do I find the date a cell was modified? Excel Excel Miscellaneous Discussions Microsoft Excel (1) Intersect (1) Error (1) Excel (1) Application.EnableEvents (1) Worksheet (1) GoTo (1) Alt (1) You do not unless you Sub Worksheet_Change(ByVal Target As Range) On Error GoTo ws_exit: Application.EnableEvents = False If Not Intersect(Target, Me.Range("A1:A10")) Is Nothing Then With Target If .Value <> "" Then .Offset(0 that worksheet module. Edit the range A1:A10 to suit. Alt + q to return to Excel. Gord Dibben MS Excel MVP keywords: How, do, I, find, the, date, a, cell, was
How do i click on a cell for a tick symbol to appear & dissapear? Excel As in checking things off (a tick) in that particular column. . .I am using microsoft excel 2003. Thanks Excel Worksheet Discussions Microsoft Excel (1) Intersect (1) Excel (1) Error (1) Application.EnableEvents (1) Worksheet (1) BeforeDoubleClick (1) GoTo (1) One way is
Negative Values in Excel Excel Is it possible to have a series of cells automatically add the negative sign in front of an inputed number? Excel Miscellaneous Discussions Microsoft Excel (1) Target.Cells.Count (1) Intersect (1) Excel (1) Error (1) Application.EnableEvents (1) VB (1) Target.Value (1) Either by formula: = -A1 range, use Paste Special, Operation Multiplication, to multiply the range by -1. - Jon - -- -- -- Jon Peltier, Microsoft Excel MVP Tutorials and Custom Solutions Peltier Technical Services, Inc. - http: / / PeltierTech.com _ __ __ __ This
How to stop opening outlook in excel Excel How to stop opening outlook in excel when clicking into a cell that contains an email address. Excel Miscellaneous Discussions Microsoft Excel (1) AutoCorrect (1) Cell.Hyperlinks.Delete (1) Hyperlinks (1) Intersect (1) Excel (1) Selection.SpecialCells (1) Macro (1) Well, you can stop Excel from automatically converting email
Can i restrict input in a cell until previous cell populated? Excel Excel Miscellaneous Discussions Microsoft Excel (1) Intersect (1) Application.EnableEvents (1) Detailsthan (1) You most likely will remain frustrated if you cannot ByVal Target As Range) Const WS_RANGE As String = "B1:B10" '< = = change to suit If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then Application.EnableEvents = False With Target If .Offset(0 ClearContents End If End With End If Application.EnableEvents = True End Sub Gord Dibben MS Excel MVP keywords: Can, i, restrict, input, in, a, cell, until, previous, cell, populated?