Solved

loosing leading zeros in excel/csv import

Posted on 2007-03-19
8
1,545 Views
Last Modified: 2013-11-25
Hi everyone.
I have another one of those leading zero problems with excel and csv files. I currently have a script that runs and pulls in a csv file, cuts half the fields out, shottens one of the field lengths and then saves the file again. It runs automatically on an entire folder using a wildcard for the filename.
Today we had a new problem in that one of the fields started with a zero and excel removed it, which caused major plant faliure because the resulting files are combined and then read into a plc for build info.

Anyone know how to read in the csv and keep the zero?

this is the script currently used:

rivate Sub Workbook_Open()
Application.Visible = True
Dim FileName As String
           Const Folder = "C:\process"
               FileName = Dir(Folder & "\*.txt")
            Do While FileName <> ""
                Application.Workbooks.Open Folder & "\" & FileName
                ActiveWorkbook.Sheets(1).Range("C:AH").Delete
                    Range("D1").Select
                    ActiveCell.FormulaR1C1 = "=MID(RC[-1],1,50)"
                    Range("D1").Select
                    Selection.Copy
                    Range("C1").Select
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                        :=False, Transpose:=False
                    Range("D1").Select
                    Application.CutCopyMode = False
                    Selection.ClearContents
                ActiveWorkbook.Close True
                FileName = Dir
            Loop
Application.Quit
End Sub


any help appreciated.
Aidy.
0
Comment
Question by:Aidy_B
8 Comments
 
LVL 11

Expert Comment

by:cup
ID: 18754285
Can we have a look at a few lines of the csv file where the zero gets stripped?

Basically what is going in and what is coming out.  A few lines will do - we don't need all the data.
0
 

Author Comment

by:Aidy_B
ID: 18757825
yep, here is the file before opening with excel, and the 'rev1' version is after saving as csv in excel.
Even when only just opened for the first time in excel, i cannot retrieve the leading zero using any change in ormatting for that column.

i will postr the links when i get home later when i can ftp the files up somewhere

Thanks, Aidy.
0
 

Author Comment

by:Aidy_B
ID: 18762430
ok, didnt get round to ftp'ing the files up somewhere last night so thought i would suggest a different approach.

I have tried this and it produces exactly the same issues so will demonstrate what i am going on about.

Open notepad and copy/paste the following line:

09073641,06074261,T3---.S4ML-FDPM

then save it as a csv with the filename '09073641.csv'

(Although i acknowledge most of the above details dont have to be as shown to simulate the problem, it will keeps everyones tests identical)

now when that file is double clicked, or open by selecting 'Open using Excel', the leading zero in the first 2 field has gone, and no amount of re-formatting at that point will bring them back. So regardless of how the file is then saved, i need to find out how to change the formating of the fields before it opens the file and removes the zeros. as you can see from the script, this is an automated process which works its way through an entire directory of files and as such, any solution needs to be compatible with this method of working.

thanks,
Aidy.
0
 
LVL 1

Expert Comment

by:GKLSOL
ID: 18806490
Use xstarter
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 65

Expert Comment

by:RobSampson
ID: 18895241
Aidy,
Usually Excel does it's own formatting based on a Cell Format.
You could try setting the Cell format to Text before / as / after you edit it.
Try something like:
   Cells.Select
   Selction.NumberFormat = "@"
This should stop Excel from autoformatting the contents.
Also try opening the csv file in Notepad after it is saved, before you open it again in Excel, and
you will have a better idea of whether the zeros are stripped as it is saved, or after it is re-opened.

Regards,

Rob.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 18956137
Aidy,
I have tried your solution of saving the sample data to a CSV file.
It seems that you cannot get it to work with a CSV extension, because Excel treats them differently.  If you change it to a TXT extension, when you open it in Excel, you get the Text File Import wizard, which allows you to select a) the field delimiting character, and b) the field data type.
Therefore, if you programmatically change the extension to a TXT through:
objFSO.CopyFile strCSVFile, strTXTFile, True
Then you can use this to open the text file in TEXT field format, which keeps the zeros.
Const xlDelimited = 1
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.OpenText _
    strFileOutputPath,,,xlDelimited,,,False,False,True,,,,Array(Array(1, 2), Array(2, 2), Array(3, 2))

Then probably just delete the TXTfile with:
objFSO.DeleteFile strTXTFile, True

Hope that helps,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 18956141
FYI, it is the 2 as the second parameter in the Arrays that specifies the Text data type.  If the second value was a 1, it would be "general" which would remove leading zeros.  If it were 3, that's a date format, etc, etc.

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 18965123
Aidy, I hope my previous post has helped you.  The solution worked for me while testing.

Rob.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now