Solved

Alternative to VB Format function

Posted on 1998-10-22
4
253 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
ID: 1441218
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
ID: 1441219
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
ID: 1441220
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
ID: 1441221
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

821 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