View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Alan Alan is offline
external usenet poster
 
Posts: 152
Default Adding Header to CSV

Might I ask, what program is creating the CSV file?

You could set up a template with the header already in it, or the code to
add the headers can be included in the macro.

Yes, you can open a text file and use the value, (CSV source file
directory), with a macro

Workbooks.OpenText Filename:= _
"YourDirectorPath&\YourFileLocater.txt", Origin:=437 _
, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(0, 1), _
TrailingMinusNumbers:=True

The path will be in A1. Setting a path variable, this can be used to open
the CSV file then closing the text file:

Dim csvPath As String
csvPath = Range("A1").Value
ActiveWorkbook.Close False

Opens the CSV file.

Workbooks.Open Filename:= csvPath


The text file has to have the exact path & filename included.

The rest is a matter of copying the data into the template or just adding a
header. Not difficult.


Alan


"The only dumb question is a question left unasked."


"mr_bill" wrote:

It would be difficult to get the program to put headers on, there are date
and numeric fields and the headers would be text.

Would it be possible to create a template that would auto import the csv? I
could have the full path and file name written to a text file that could be
read by a macro to import the csv file.

"Alan" wrote:

mr_bill,

I would look into the "program that produces a CSV file" first to see if
there are any header options.

If not, since the program opens an instance of Excel, you could not
automatically format it unless you have Excel to perform this on start up,
which would affect every file you open.

You could write a macro, and make it available on an Excel toolbar, that you
could click on when the file opens. This macro could set the headers for you.
The only drawback to this is if you have an instance of Excel open at the
time the CSV instance opens, it will error on trying to open the Personal.xls
file for a second time.

Just some thoughts,

Alan


"The only dumb question is a question left unasked."



"mr_bill" wrote:

I have a program that produces a CSV file without headers. When the program
finishes creating the file, it launches excel and opens the CSV. Is there a
way that I can have headers added to the file?