Solved

GetFormat module

Posted on 2013-06-11
7
215 Views
Last Modified: 2013-06-12
Experts,

I have this function that rounds large numbers.  It  works fine except the billions are not rounded properly.

ie:  2,699,458,000.00
rounds to 2.699458B

Public Function GetFormat(varField) As String
'USED WITH AN UNBOUND FIELD ON THE RPT AND APPENDS MM OR B
    Select Case Len(varField)
    Case 7 To 9
        GetFormat = FormatNumber(varField / 1000000, 0) & "MM"
    Case 10
        GetFormat = varField / 1000000000 & "B"
End Select
End Function

how can I make
2,699,458,000.00
round to 2.7B?

thank you
0
Comment
Question by:pdvsa
7 Comments
 

Author Comment

by:pdvsa
ID: 39239193
I use the function like this:
AmtOverlay: GetFormat([contractamt])
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39239251
Please explain what "rounded properly" would be :)
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 39239257
use this function


Public Function GetFormat(varField) As String
'USED WITH AN UNBOUND FIELD ON THE RPT AND APPENDS MM OR B
varField = CDec(Replace(varField, ",", ""))
    Select Case Len(varField)
    Case 7 To 9
        GetFormat = FormatNumber(varField / 1000000, 0) & "MM"
    Case 10 To 12
        GetFormat = FormatNumber(varField / 1000000000, 1) & "B"
End Select
End Function
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:pdvsa
ID: 39239294
Capricorn,

thanks.  do i need to handle nulls?  I get a runtime error 94 "Invalid use of Null".  The debugger highlights the varField line.  

thank you.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 400 total points
ID: 39239732
Public Function GetFormat(varField) As String
'USED WITH AN UNBOUND FIELD ON THE RPT AND APPENDS MM OR B

if varField & ""="" then GetFormat="": Exit Function

varField = CDec(Replace(varField, ",", ""))
    Select Case Len(varField)
    Case 7 To 9
        GetFormat = FormatNumber(varField / 1000000, 0) & "MM"
    Case 10 To 12
        GetFormat = FormatNumber(varField / 1000000000, 1) & "B"
End Select
End Function
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39240501
Just replace FormatNumber with Format and brush up a little:
Public Function GetFormat(ByVal varField As Variant) As String

' USED WITH AN UNBOUND FIELD ON THE RPT AND APPENDS MM OR B

    If IsNumeric(varField) Then
        Select Case Len(varField)
            Case 7 To 9
                GetFormat = Format(varField / 10 ^ 6, 0) & "MM"
            Case 10 To 12
                GetFormat = Format(varField / 10 ^ 9, 0) & "B"
        End Select
    End If

End Function

Open in new window

/gustav
0
 

Author Comment

by:pdvsa
ID: 39242042
Capricorn, very nice.  it rounded 2,699,458,000.00  to 2.7B, which is exactly what I needed.  

Gustav:  that worked too (but it did round the 2.7B to 3B).  

thank you
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now