Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Representing numbers with K, M, G sufix using a macro

I need help with code to convert a number, for example 2000000, into 2 M or
2000000000 into 2 G etc.

Thanks.

Farooq
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 298
Default Representing numbers with K, M, G sufix using a macro

dim s as string

select case application.worksheetfunction.log10(x)
case =9: s = round(x/1e9,2) & "G"
case =6: s = round(x/1e6,2) & "M"
case =3: s = round(x/1e3,2) & "K"
case else: s = cstr(x)
end select

(untested)


"Farooq Sheri" wrote in message
...
I need help with code to convert a number, for example 2000000, into 2 M or
2000000000 into 2 G etc.

Thanks.

Farooq



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Representing numbers with K, M, G sufix using a macro

You can use this event code to do that (it also includes a T for terabytes,
just in case). To implement this 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"
ElseIf Target.Value < 999500 Then
Target.NumberFormat = "0.000, \K"
ElseIf Target.Value < 999500000 Then
Target.NumberFormat = "0.000,, \M"
ElseIf Target.Value < 999500000000# Then
Target.NumberFormat = "0.000,,, \G"
Else
Target.NumberFormat = "0.000,,,, \T"
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 (use Cells instead of a Range call if you want this
functionality to apply everywhere on the worksheet). Also, since you didn't
mention it, I chose to display 3 decimal places after the decimal point for
numbers with a suffix. After implementing the above code, 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...

Selection.Formula = Selection.Formula

--
Rick (MVP - Excel)


"Farooq Sheri" wrote in message
...
I need help with code to convert a number, for example 2000000, into 2 M or
2000000000 into 2 G etc.

Thanks.

Farooq


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Representing numbers with K, M, G sufix using a macro

By the way, I chose to add the suffixes using NumberFormat in order to leave
the numbers in the cells as numbers so that they could continue to be used
in calculations.

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
You can use this event code to do that (it also includes a T for
terabytes, just in case). To implement this 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"
ElseIf Target.Value < 999500 Then
Target.NumberFormat = "0.000, \K"
ElseIf Target.Value < 999500000 Then
Target.NumberFormat = "0.000,, \M"
ElseIf Target.Value < 999500000000# Then
Target.NumberFormat = "0.000,,, \G"
Else
Target.NumberFormat = "0.000,,,, \T"
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 (use Cells instead of a Range call if you want this
functionality to apply everywhere on the worksheet). Also, since you
didn't mention it, I chose to display 3 decimal places after the decimal
point for numbers with a suffix. After implementing the above code, 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...

Selection.Formula = Selection.Formula

--
Rick (MVP - Excel)


"Farooq Sheri" wrote in message
...
I need help with code to convert a number, for example 2000000, into 2 M
or
2000000000 into 2 G etc.

Thanks.

Farooq



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 118
Default Representing numbers with K, M, G sufix using a macro

Function NumSuffix(MyNum as Long) as String
Dim Result as String
if MyNum Mod 1E12 = 0 then Result = CStr(Mynum/1E12) & " T"
if MyNum Mod 1E9 = 0 then Result = CStr(Mynum/1E9) & " G"
if MyNum Mod 1E6 = 0 then Result = CStr(Mynum/1E6) & " M"
if MyNum Mod 1E3 = 0 then Result = CStr(Mynum/1E3) & " K"
NumSuffix = Result
End Function
--
If this helps, please click "Yes"
<<<<<<<<<<<<


"Farooq Sheri" wrote:

I need help with code to convert a number, for example 2000000, into 2 M or
2000000000 into 2 G etc.

Thanks.

Farooq



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 523
Default Representing numbers with K, M, G sufix using a macro

You'll have to reverse the order of those ifs...

2,000,000,000,000 mod 1E12 = 0 so result = 2T
2,000,000,000,000 mod 1E9 = 0 so result = 2000G
etc.

Sam

"BSc Chem Eng Rick" wrote:

Function NumSuffix(MyNum as Long) as String
Dim Result as String
if MyNum Mod 1E12 = 0 then Result = CStr(Mynum/1E12) & " T"
if MyNum Mod 1E9 = 0 then Result = CStr(Mynum/1E9) & " G"
if MyNum Mod 1E6 = 0 then Result = CStr(Mynum/1E6) & " M"
if MyNum Mod 1E3 = 0 then Result = CStr(Mynum/1E3) & " K"
NumSuffix = Result
End Function
--
If this helps, please click "Yes"
<<<<<<<<<<<<


"Farooq Sheri" wrote:

I need help with code to convert a number, for example 2000000, into 2 M or
2000000000 into 2 G etc.

Thanks.

Farooq

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
bar on chart isn't representing correctly rrupp Charts and Charting in Excel 2 February 4th 08 11:30 PM
Can I have words representing numbers in a formula? PeterM Excel Discussion (Misc queries) 4 August 7th 06 09:29 PM
how to convert whole numbers representing seconds to mm:ss Elvin Excel Worksheet Functions 2 March 6th 06 05:49 PM
Representing symbols in charts Symbols in Charts Charts and Charting in Excel 1 July 14th 05 09:21 PM
FORMULAS FOR ADDING LETTERS REPRESENTING NUMBERS koolone Excel Discussion (Misc queries) 1 April 8th 05 12:39 PM


All times are GMT +1. The time now is 06:56 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"