creating a text record from a number preserving the leading zero




For internal reasons i need to consistently produce a text report,
comma separated with quotes. One of the fields must be a number
"0234" beginning with zero... but no matter whether i use quotes or
use the format function it always results in "233".

I have tried:
vMacBlotter(i, 3) = "0233"


Format("0233", "")

which both appear to enter the datum correctly to the variant

However when I convert to text with commas it comes out as "233".
Please help:

Sub QuoteCommaExport()
' Dimension all variables.
Dim DestFile As String
Dim FileNum As Integer
Dim ColumnCount As Integer
Dim RowCount As Integer

' Prompt user for destination file name.
'DestFile = InputBox("Enter the destination filename" _
& Chr(10) & "(with complete path):", "Quote-Comma
DestFile =

' Obtain next free file handle number.
FileNum = FreeFile()

' Turn error checking off.
On Error Resume Next

' Attempt to open destination file for output.
Open DestFile For Output As #FileNum

' If an error occurs report it and end.
If Err <> 0 Then
MsgBox "Cannot open filename " & DestFile
End If

' Turn error checking on.
On Error GoTo 0

' Loop for each row in selection.
For RowCount = 1 To Selection.Rows.count

' Loop for each column in selection.
For ColumnCount = 1 To Selection.Columns.count

' Write current cell's text to file with quotation
Print #FileNum, """" & Selection.Cells(RowCount,
ColumnCount).Text & """";

' Check if cell is in last column.
If ColumnCount = Selection.Columns.count Then
' If so, then write a blank line.
Print #FileNum,
' Otherwise, write a comma.
Print #FileNum, ",";
End If
' Start next iteration of ColumnCount loop.
Next ColumnCount
' Start next iteration of RowCount loop.
Next RowCount

' Close destination file.
Close #FileNum
End Sub


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question