Solved

Using Format in loops

Posted on 2001-07-29
7
137 Views
Last Modified: 2010-05-02
I have a loop

for a=1 to 5

printer.print format(firstvalue(a),"####.##"), format(secondvalue(a),"###.00"), format(thirdvalue(a)," ###.##")

next a

How do I get the following layout using format?

   7.89      0.34    6.12
1245.00   3452.56   34.00
    .01     70.00    1.34

etc

also variation for no decimal places in say firstvalue?

Thank you
0
Comment
Question by:bemson
  • 4
  • 3
7 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6332386
Use this mask: "####.00"
0
 

Author Comment

by:bemson
ID: 6332430
format shown after submission not the same as I typed it

using your suggestion for following values

78,2456,0.12
23.34,0.12,346

I get the following
- represents a space in case of what I type is not clear

78.00,2456.00,.12
23.34,.12----,346.00

- represents a space in case of what I type is not clear

I want the following


78.00,2456.00,----.12
23.34,----.12, 346.00

thank you

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6332470
You can use something like this:

Private Sub Command1_Click()
Debug.Print EMFormat(7.89, 7) & "," & EMFormat(0.34, 7) & "," & EMFormat(6.12, 7)
End Sub

Private Function EMFormat(ByVal psngValue As Single, ByVal pbytLen As Byte) As String
    EMFormat = Right$(Space$(pbytLen) & Format$(psngValue, "####.00"), pbytLen)
End Function
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:bemson
ID: 6332509
OK,

However I would like to vary the function capabilty
by changing 7 to 4 to get the following
for the first two values, ie 78 first line, 23 second line

Printer.Print EMFormat(78, 4), EMFormat(2456, 7), EMFormat(0.12, 7)
Printer.Print EMFormat(23.34, 4), EMFormat(0.12, 7), EMFormat(346, 7)

although 23 should be 24 if figure was 23.78

78,2456.00,---.12
23,----.12,346.00

what I get is:

8.00,2456.00,---.12
3.34,----.12,346.00

thank you
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 6332552
In this case, use this:

Private Sub Command1_Click()
Debug.Print EMFormat2(7.89, 4) & "," & EMFormat(0.34, 7) & "," & EMFormat(6.12, 7)
End Sub

Private Function EMFormat(ByVal psngValue As Single, ByVal pbytLen As Byte) As String
   EMFormat = Right$(Space$(pbytLen) & Format$(psngValue, "####.00"), pbytLen)
End Function

Private Function EMFormat2(ByVal psngValue As Single, ByVal pbytLen As Byte) As String
   EMFormat = Right$(Space$(pbytLen) & Format$(psngValue, "###0"), pbytLen)
End Function
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6332602
Why a B when I give you exactly what you want?
0
 

Author Comment

by:bemson
ID: 6336017


I now have to figure out how to use your function to perform a function that vb should have for all other combinations with both format and number of characters,
in any case by using your function, if Format2 and using "###0" then default obviously would be 4, so why specify pbytLen?

if I pass 346.3,2 to your function I get 46, no error that it should be 346

in dos with qbasic
the statement "print using "####", firstvalue
provided the spacing

I would have got " 346"



obviously whoever designed vb has missed the point of formatting

basically I (and others) need a single function to provide spaces to properly format numbers

So for every format I need I have to create a new function for every possibility, both for number formatting & how many numbers to display

If you have a better solution I am willing to give 100
points and an A

even so, thanks for your help
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
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…

837 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