Solved

Alternative to VB Format function

Posted on 1998-10-22
4
250 Views
Last Modified: 2010-05-18
VB5

I need a function that serves the same purpose as the intrinsic VB Format function but with slightly different behavior.

Specifically,  when the format string is "$ #,###,##0.00" and the value is 1.00 then the formatted result is "$        1.00"

VB would give me "$ 1.00"

I need a fully justified string so that my columns line up correctly. I'm looking for a easy solution other than writing my own Format or Justification function.
0
Comment
Question by:armg
  • 2
4 Comments
 
LVL 15

Expert Comment

by:ameba
Comment Utility
You need a function, but without writing your own function?
Since format function does not do what you want, you will need your own function which will do it!
If you must not define new function e.g. myFormat, you can do all inline. But this is not easy solution! Too much typing.
So you need your own function, do you?
0
 
LVL 12

Accepted Solution

by:
mark2150 earned 100 total points
Comment Utility
Don't use FORMAT. Try this:

Debug.Print MyFormat( 123.45, "$###,###,###.##" )
Debug.Print MyFormat( 12.45, "$###,###,###.##" )
Debug.Print MyFormat( 1.45, "$###,###,###.##" )
Debug.Print MyFormat( 123456789.01, "$###,###,###.##" )

PUBLIC FUNCTION MyFormat( MyValue as Double, Template as String ) AS STRING
MyFormat = right$( space$( len( template$ ) ) + format( MyValue, Template$ ), len( template$ ) )
END FUNCTION

0
 
LVL 12

Expert Comment

by:mark2150
Comment Utility
BTW: if you don't want the $ to float call it like this:

Debug.Print "$"; MyFormat( 123.45, "###,###,###.##" )
Debug.Print "$"; MyFormat( 12.45, "###,###,###.##" )
Debug.Print "$"; MyFormat( 1.45, "###,###,###.##" )
Debug.Print "$"; MyFormat( 123456789.01, "###,###,###.##" )

(yawn)

M

0
 
LVL 1

Expert Comment

by:lawnranger
Comment Utility
Or how about...

Dim S As String
Dim m As Currency
m = 3.12
S = "$" + Format$(Format$(m, "###,###,###,##0.00"), "@@@@@@@@@@")

One more point; you will have to use a fixed width font like Courier or you'll still have misaligned columns.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 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

13 Experts available now in Live!

Get 1:1 Help Now