Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 683
Default Text with leading zeros being converted to numbers

I have the Auto_Open macro below in a workbook (referred to in my code as
TemplateName) that opens a source CSV file, brings the contents into the
current sheet, does some reorganization, and finally saves it to another CSV
file. for import into another application that cannot handle some of the raw
formatting in the source CSV file.

I have a problem: data has now begun to appear with things like "001"
instead of "1" in some fields in the source CSV file. If I open it in Excel,
the "001" appears as "1", but in Notepad it correctly shows "001". The macro
below ends up converting "001" to "1" in the output CSV file. How can I
ensure that the macro does not convert these numeric-like strings to numbers?


Sub Auto_Open()
Dim DataPath As String
Dim FileName As String
Dim TemplateName As String
Dim ImportFileName As String

DataPath = "J:\HartWorks\TruckMateSupplementals\FleetOne"
TemplateName = "FleetOneTemplate.xls"
ImportFileName = "FleetOneImport.csv"
FileName = "160071.csv"
'
' Copy header into fuel file and save as Excel sheet
'
Workbooks.Open FileName:=DataPath & "\" & FileName
Windows(TemplateName).Activate
Rows("1").Select
Selection.Copy
Windows(FileName).Activate
Range("A1").Select
Selection.Insert Shift:=xlDown
Cells.Select
Selection.Replace What:="+", Replacement:="", LookAt:=xlPart,
SearchOrder:=xlByRows
Selection.Replace What:="=", Replacement:="", LookAt:=xlPart,
SearchOrder:=xlByRows
Range("C:C,E:E,H:K,Q:Q,S:U,W:X,AA:AM,AN:AQ,AS:AT,A V:AW").Select
Selection.Delete Shift:=xlToLeft
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Application.CutCopyMode = False
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=DataPath & "\" & ImportFileName,
FileFormat:=xlCSV, Password:="", WriteResPassword:="",
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWindow.Close
Application.Quit
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Text with leading zeros being converted to numbers

Do the same thing you would do manually:

1. rename the file from .csv to .txt
2. tell the Import Wizard that the field is Text

The file being named .csv shortcircuits the Import Wizard
--
Gary''s Student - gsnu200732


"Brian" wrote:

I have the Auto_Open macro below in a workbook (referred to in my code as
TemplateName) that opens a source CSV file, brings the contents into the
current sheet, does some reorganization, and finally saves it to another CSV
file. for import into another application that cannot handle some of the raw
formatting in the source CSV file.

I have a problem: data has now begun to appear with things like "001"
instead of "1" in some fields in the source CSV file. If I open it in Excel,
the "001" appears as "1", but in Notepad it correctly shows "001". The macro
below ends up converting "001" to "1" in the output CSV file. How can I
ensure that the macro does not convert these numeric-like strings to numbers?


Sub Auto_Open()
Dim DataPath As String
Dim FileName As String
Dim TemplateName As String
Dim ImportFileName As String

DataPath = "J:\HartWorks\TruckMateSupplementals\FleetOne"
TemplateName = "FleetOneTemplate.xls"
ImportFileName = "FleetOneImport.csv"
FileName = "160071.csv"
'
' Copy header into fuel file and save as Excel sheet
'
Workbooks.Open FileName:=DataPath & "\" & FileName
Windows(TemplateName).Activate
Rows("1").Select
Selection.Copy
Windows(FileName).Activate
Range("A1").Select
Selection.Insert Shift:=xlDown
Cells.Select
Selection.Replace What:="+", Replacement:="", LookAt:=xlPart,
SearchOrder:=xlByRows
Selection.Replace What:="=", Replacement:="", LookAt:=xlPart,
SearchOrder:=xlByRows
Range("C:C,E:E,H:K,Q:Q,S:U,W:X,AA:AM,AN:AQ,AS:AT,A V:AW").Select
Selection.Delete Shift:=xlToLeft
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Application.CutCopyMode = False
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=DataPath & "\" & ImportFileName,
FileFormat:=xlCSV, Password:="", WriteResPassword:="",
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWindow.Close
Application.Quit
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 683
Default Text with leading zeros being converted to numbers

Thanks. That makes sense. I see when I re-recorded the macro that this
results in Array (1,1), Array (2,1), Array (3,2) where the first digit is the
column & the second digit is the format (1 = General, 2 = Text).

Now I just need to use the Name statement to rename the source file from
..csv to .txt. I've done that a lot in Access before, and it looks like VBA is
the same in Excel on this point.

"Gary''s Student" wrote:

Do the same thing you would do manually:

1. rename the file from .csv to .txt
2. tell the Import Wizard that the field is Text

The file being named .csv shortcircuits the Import Wizard
--
Gary''s Student - gsnu200732


"Brian" wrote:

I have the Auto_Open macro below in a workbook (referred to in my code as
TemplateName) that opens a source CSV file, brings the contents into the
current sheet, does some reorganization, and finally saves it to another CSV
file. for import into another application that cannot handle some of the raw
formatting in the source CSV file.

I have a problem: data has now begun to appear with things like "001"
instead of "1" in some fields in the source CSV file. If I open it in Excel,
the "001" appears as "1", but in Notepad it correctly shows "001". The macro
below ends up converting "001" to "1" in the output CSV file. How can I
ensure that the macro does not convert these numeric-like strings to numbers?


Sub Auto_Open()
Dim DataPath As String
Dim FileName As String
Dim TemplateName As String
Dim ImportFileName As String

DataPath = "J:\HartWorks\TruckMateSupplementals\FleetOne"
TemplateName = "FleetOneTemplate.xls"
ImportFileName = "FleetOneImport.csv"
FileName = "160071.csv"
'
' Copy header into fuel file and save as Excel sheet
'
Workbooks.Open FileName:=DataPath & "\" & FileName
Windows(TemplateName).Activate
Rows("1").Select
Selection.Copy
Windows(FileName).Activate
Range("A1").Select
Selection.Insert Shift:=xlDown
Cells.Select
Selection.Replace What:="+", Replacement:="", LookAt:=xlPart,
SearchOrder:=xlByRows
Selection.Replace What:="=", Replacement:="", LookAt:=xlPart,
SearchOrder:=xlByRows
Range("C:C,E:E,H:K,Q:Q,S:U,W:X,AA:AM,AN:AQ,AS:AT,A V:AW").Select
Selection.Delete Shift:=xlToLeft
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Application.CutCopyMode = False
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=DataPath & "\" & ImportFileName,
FileFormat:=xlCSV, Password:="", WriteResPassword:="",
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWindow.Close
Application.Quit
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
Mix text and numbers with leading zeros Susan B Excel Worksheet Functions 3 September 17th 08 11:03 PM
Sort "text" numbers with and without leading zeros as numbers T.K. Excel Discussion (Misc queries) 1 February 10th 08 04:19 PM
Leading zeroes get dropped when converted to text Jason Grunert Excel Discussion (Misc queries) 8 August 4th 06 09:42 PM
sort numbers leading zeros l smith Excel Discussion (Misc queries) 2 June 8th 05 02:05 AM
save text field w/ leading zeros in .csv format & not lose zeros? Ques Excel Discussion (Misc queries) 1 May 4th 05 06:21 PM


All times are GMT +1. The time now is 11:35 PM.

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

About Us

"It's about Microsoft Excel"