Solved

Alternative to VB Format function

Posted on 1998-10-22
4
252 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

803 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