Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
ww ww is offline
external usenet poster
 
Posts: 6
Default creating a text record from a number preserving the leading zero

Hello

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"

and

Format("0233", "")

which both appear to enter the datum correctly to the variant
vMacBlotter.

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
Exporter")
DestFile =
"C:\ACME\Trading\MacBlotters\ACME200408260000. csv"

' 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
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
marks.
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,
Else
' 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default creating a text record from a number preserving the leading zero

Hi
try
vMacBlotter(i, 3).value=format(variable,"0000")

--
Regards
Frank Kabel
Frankfurt, Germany

"ww" schrieb im Newsbeitrag
om...
Hello

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"

and

Format("0233", "")

which both appear to enter the datum correctly to the variant
vMacBlotter.

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
Exporter")
DestFile =
"C:\ACME\Trading\MacBlotters\ACME200408260000. csv"

' 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
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
marks.
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,
Else
' 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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy a number into a text cell, keeping leading zeros? Basher Bates Charts and Charting in Excel 2 February 23rd 08 03:26 PM
leading zeros when uploading a number from text file mc01234 Excel Discussion (Misc queries) 1 May 18th 06 08:47 PM
zero supress leading zeros when chg format from text to number HeatherO Excel Worksheet Functions 4 February 27th 05 11:11 PM
Number to text with leading zero German[_5_] Excel Programming 2 August 2nd 04 11:43 PM
Force a Number to be Formatted as Text (keep leading 0's) Tom Ogilvy Excel Programming 1 July 27th 04 07:29 PM


All times are GMT +1. The time now is 01:29 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"