#1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35
Default Decimal function

I'm using the following function to format the number of decimals to
show:

Function FormatDecimal(strValue As String) As String
On Error Resume Next

Dim arrArray As Variant
arrArray = Split(strValue, ",")

If Len(arrArray(1)) = 0 Then FormatDecimal = arrArray(0)
If Len(arrArray(1)) = 1 Then FormatDecimal = FormatNumber(strValue,
1)
If Len(arrArray(1)) = 2 Then FormatDecimal = FormatNumber(strValue,
2)
If Len(arrArray(1)) = 3 Then FormatDecimal =
FormatNumber(strValue, 3)
End Function

If strValue = "115" the above function returns "115,000" which is
wrong. I want it to return "115". The reason is that Len(arrArray(1)) =
"Index outside interval" in this case.

Can someone please help me?

Regards,

S

  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,646
Default Decimal function

Try this:
Function FormatDecimal(strValue As String) As String
p = InStr(1, strValue, ",")
dec = IIf(p = 0, 0, Len(strValue) - p)
FormatDecimal = FormatNumber(strValue, WorksheetFunction.Min(3, dec))
End Function

Regards,
Stefi

€ť ezt Ă*rta:

I'm using the following function to format the number of decimals to
show:

Function FormatDecimal(strValue As String) As String
On Error Resume Next

Dim arrArray As Variant
arrArray = Split(strValue, ",")

If Len(arrArray(1)) = 0 Then FormatDecimal = arrArray(0)
If Len(arrArray(1)) = 1 Then FormatDecimal = FormatNumber(strValue,
1)
If Len(arrArray(1)) = 2 Then FormatDecimal = FormatNumber(strValue,
2)
If Len(arrArray(1)) = 3 Then FormatDecimal =
FormatNumber(strValue, 3)
End Function

If strValue = "115" the above function returns "115,000" which is
wrong. I want it to return "115". The reason is that Len(arrArray(1)) =
"Index outside interval" in this case.

Can someone please help me?

Regards,

S


  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35
Default Decimal function

Thank's a lot Stefi! Looks like this is a really neat function.

Some users have "." instead of "," as decimal separator. Is there a way
to also handle this?

Regards,

S

Stefi skrev:

Try this:
Function FormatDecimal(strValue As String) As String
p = InStr(1, strValue, ",")
dec = IIf(p = 0, 0, Len(strValue) - p)
FormatDecimal = FormatNumber(strValue, WorksheetFunction.Min(3, dec))
End Function

Regards,
Stefi

" ezt írta:

I'm using the following function to format the number of decimals to
show:

Function FormatDecimal(strValue As String) As String
On Error Resume Next

Dim arrArray As Variant
arrArray = Split(strValue, ",")

If Len(arrArray(1)) = 0 Then FormatDecimal = arrArray(0)
If Len(arrArray(1)) = 1 Then FormatDecimal = FormatNumber(strValue,
1)
If Len(arrArray(1)) = 2 Then FormatDecimal = FormatNumber(strValue,
2)
If Len(arrArray(1)) = 3 Then FormatDecimal =
FormatNumber(strValue, 3)
End Function

If strValue = "115" the above function returns "115,000" which is
wrong. I want it to return "115". The reason is that Len(arrArray(1)) =
"Index outside interval" in this case.

Can someone please help me?

Regards,

S



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
Custom functions calculating time arguments Help Desperate Bill_De Excel Worksheet Functions 12 April 25th 06 02:22 AM
How can I use Excel to solve an equation? titina Excel Worksheet Functions 4 April 11th 06 11:19 PM
Date & Time mully New Users to Excel 4 May 23rd 05 11:56 AM
Automatically up date time in a cell Mark Excel Discussion (Misc queries) 5 May 12th 05 12:26 AM
Conversion SVC Excel Worksheet Functions 9 February 28th 05 02:29 PM


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