Solved

Alternative to VB Format function

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

737 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