OK
Here is some code:
Sub TestFormats()
Const fmtnum1 = "###0"
Const fmtnum2 = "###0 "
Const fmtnum3 = "###0: "
Dim i As Long
Dim j As Long
For i = 1 To 4
j = Int(10 ^ (i - 1) * Rnd()) ' Get a (relatively) random integer
Debug.Print i & ": " & j, fmtnum1 & ">>" & Format(j, fmtnum1), Right(String(Len(fmtnum1), " ") & Format(j, fmtnum1), Len(fmtnum1)),
Debug.Print fmtnum2 & ">>" & Format(j, fmtnum2), Right(String(Len(fmtnum2), " ") & Format(j, fmtnum2), Len(fmtnum2)),
Debug.Print fmtnum3 & ">>" & Format(j, fmtnum3), Right(String(Len(fmtnum3), " ") & Format(j, fmtnum3), Len(fmtnum3))
Next i
End Sub
We set up three different numeric formats - with some additional characters (as literals).
We loop through the process of
Get a random digit between 1 and 999
Format that value using each of the three format codes. (None of them work)
The only way I can find to get right justified columns is the ghastly construct in each debug.print line, viz:
Right(String(Len(fmtnum1), " ") & Format(j, fmtnum1), Len(fmtnum1))
Careful examination will reveal that there are no references to any cells on any spreadsheets! Yes - I know that Excel as a workbook can do all this in its sleep. However, I want to be able to get a series of numbers right justified within a fixed width string. Why do I want that? Just cos. It's a requirement, and I'm sufficiently pleased with VBA as a general purpose tool that I can live with most of its eccentricities. However this seems a very torturous method of getting what I can accomplish (in COBOL) with "PIC 9(4)" or PIC Z(3)9".
My simple request is "Is there a better way? If not, I can live with it (grumbling the while), but if there is a better way I'd love to learn it! (Unless it involves writing the whole report as a spreadsheet, in which case I will assume that there is no such alternative!)
Thanks, again, and sorry for the rants!
Tony
Bookmarks