?
Solved

Alternative to VB Format function

Posted on 1998-10-22
4
Medium Priority
?
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

800 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