Missing commas in csv save

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi

I have a question about saving to csv files, in some circumstances a
leading "," is being dropped and I wanted to know if there is a way
around it.

Explanation of situation first, then the specific question releated to
the situation at the end.


I have some code which copies a table from an Excel workbook, pastes
it into a new workbook, and then saves that workbook as a csv file:

Code something like:
Application.Goto Reference:=strDataRange
Selection.Copy
Workbooks.Add
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
strOutpath & "\" & strDataRange & ".csv", _
FileFormat:=xlCSV, _
CreateBackup:=False
ActiveWindow.Close SaveChanges:=False


Of particular interest is a table which has a heading row where the
first column has no header at all.
eg <blank> myFirstHeaderCol mySecondHeaderCol etc
data1 data2 data3
data4 data5 data6
etc

Normally this gets saved into a csv file as:
,myFirstHeaderCol,mySecondHeaderCol,<etc>
data1,data2,data3
data4,data5,data6
etc

The important point is that leading "," on the first line

This output is exactly what I want.



Things get interesting if this table has no data. In these cases the
range is still specified over the table, it is just the table is empty
bar the header row.

When I do the csv save in this case I get the following:
myFirstHeaderCol, mySecondHeaderCol,<etc>

So excel has dropped the first ","

I suppose that without data in the rest of the table Excel didnt know
that I wanted that first column, ie it thought it was doing me a
favour by dropping it like it also drops empty rows etc.


QUESTION:
Is there any way I can get Excel to include every comma even if
subsequent rows etc for that column are empty?

Thanks
Andy C
.



Relevant Pages

  • Re: CSV import, excel corrupts data
    ... I guess if you want to use excel, you'll have to play by its rules. ... If opening .csv files directly doesn't work for you, ... Doing the macro trick corrupts the data as described when the file is a ... empty worksheet, use the data import wizard to select the csv file, ...
    (microsoft.public.excel)
  • Re: CSV import, excel corrupts data
    ... It _does_ bring up the data import wizard. ... There really seems to be no way to accomplish this in excel. ... Doing the macro trick corrupts the data as described when the file is a ... empty worksheet, use the data import wizard to select the csv file, ...
    (microsoft.public.excel)
  • Re: Basic Program to Import a CSV file
    ... Given that the OP asked about importing a CSV file into UV, ... some Excel to MV tool to import a CSV file into UV? ... Hey, why stop at replacing the database, replace the spreadsheet tools ... The directory file may well be UV ...
    (comp.databases.pick)
  • Re: On File open - how to stop conversion of postcodes to numbers
    ... definite failing of Excel to anticipate correctly the programmers wishes. ... a space before every entry in the postcode field before the first number just ... I could write an automated macro to run the file wizard but I would forever ... When I open CSV file using Notepad the field contains "00037" so it is all ...
    (microsoft.public.excel.worksheet.functions)
  • Re: CSV import, excel corrupts data
    ... If you rename your .csv file to .txt, you can have complete control over each ... field--you can choose Text for the fields that excel thinks are dates (but ... Now Excel utterly corrupts the data and displays some numeric ... its type from standard to text and finally confirm the import at cell A1. ...
    (microsoft.public.excel)