PC Review


Reply
Thread Tools Rate Thread

Automating the conversion of CSV files to XLSX files

 
 
Chris
Guest
Posts: n/a
 
      28th Nov 2007
I have quite a few comma-separated-value (i.e., CSV) files that need to be
converted to XLSX format. Currently, I'm converting each file manually by
opening Excel, initiating an import, changing the original data type from
Fixed Width to Delimited, de-selecting the Tab delimiter, selecting the Comma
delimiter, changing some of the column data formats from General to Text (to
prevent incorrect conversion of those columns from text to numeric), and then
pressing the Finish button.

Is there any way that this process can be automated? Is there any way I
could initiate the process from a DOS command-line?
 
Reply With Quote
 
 
 
 
Jon Peltier
Guest
Posts: n/a
 
      28th Nov 2007
If the file extension is CSV, this should all happen automatically just by
opening the file, well, except for the column data formats.

Did you try recording a macro while you converted one file manually?

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Chris" <(E-Mail Removed)> wrote in message
news:BBDF9C29-3179-4DD6-9D3E-(E-Mail Removed)...
>I have quite a few comma-separated-value (i.e., CSV) files that need to be
> converted to XLSX format. Currently, I'm converting each file manually by
> opening Excel, initiating an import, changing the original data type from
> Fixed Width to Delimited, de-selecting the Tab delimiter, selecting the
> Comma
> delimiter, changing some of the column data formats from General to Text
> (to
> prevent incorrect conversion of those columns from text to numeric), and
> then
> pressing the Finish button.
>
> Is there any way that this process can be automated? Is there any way I
> could initiate the process from a DOS command-line?



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      28th Nov 2007
Thanks, Jon. The values in the first column in these CSV files are 20-digit
numeric codes that Excel is mis-interpreting and incorrectly converting to
exponential format--hence the need to avoid opening the CSV file
directly...instead overriding Excel's default behavior by using the Import
function.

Inexplicably, for these CSV files the Text Import Wizard defaults to using
the Tab character as the delimiter.

> Did you try recording a macro while you converted one file manually?


Using a macro, I've been able to automate the process for the files whose
first column contains a 20-digit code. Certain CSV files, however, have
additional columns with 20-digit codes. Rather than pick and choose which
columns to convert to Text fomat, it would be OK to convert *all* of the
columns; however, I can't figure out how to make the Text Import Wizard do
that.

Also, assuming the above can be resolved, can Excel be started from the
command-line with the name of a macro to run?
 
Reply With Quote
 
Jon Peltier
Guest
Posts: n/a
 
      28th Nov 2007
When I get a case like this, I usually import the data into VBA, process it,
and dump it into the worksheet.

This is going to be a little slow, since it outputs one row at a time, and
it's not totally automatic, but it's a start.

Sub inputTXTfile()
' input text file line by line without interpreting html tabs
' open new sheet first and select home cell

Dim linein As String, fh As Integer, FileName As String, lineNum As
Double
dim vArray as Variant

FileName = Application.GetOpenFilename
If FileName = "False" Then Exit Sub
Application.ScreenUpdating = False
lineNum = 0
fh = FreeFile
Workbooks.Add

Open FileName For Input As fh
Do Until EOF(fh)
Line Input #fh, linein
vArray = Split(linein, ",")
With ActiveCell.Offset(lineNum, 0).Resize(, UBound(vArray) + 1)
.Value = linein
.NumberFormat = "@"
End With
lineNum = lineNum + 1
Loop
Close #fh

Application.ScreenUpdating = True
End Sub


- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Chris" <(E-Mail Removed)> wrote in message
news:0B8A7F5B-FDF7-4544-B346-(E-Mail Removed)...
> Thanks, Jon. The values in the first column in these CSV files are
> 20-digit
> numeric codes that Excel is mis-interpreting and incorrectly converting to
> exponential format--hence the need to avoid opening the CSV file
> directly...instead overriding Excel's default behavior by using the Import
> function.
>
> Inexplicably, for these CSV files the Text Import Wizard defaults to using
> the Tab character as the delimiter.
>
>> Did you try recording a macro while you converted one file manually?

>
> Using a macro, I've been able to automate the process for the files whose
> first column contains a 20-digit code. Certain CSV files, however, have
> additional columns with 20-digit codes. Rather than pick and choose which
> columns to convert to Text fomat, it would be OK to convert *all* of the
> columns; however, I can't figure out how to make the Text Import Wizard do
> that.
>
> Also, assuming the above can be resolved, can Excel be started from the
> command-line with the name of a macro to run?



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I batch convert 97-2003 .xls files to 2007 .xlsx files =?Utf-8?B?RGF2ZSBOdXR0YWxs?= Microsoft Excel Misc 4 3rd Aug 2009 11:38 PM
save xlsx files Kim Carlsen Microsoft Excel Programming 1 12th Jun 2009 04:41 PM
.xls files not deleted after conversion to .xlsx; help please Tschurin Microsoft Excel Misc 13 13th Nov 2008 02:31 PM
Help with xlsx files Jorge Cervantes Microsoft Excel New Users 1 25th Feb 2008 07:16 PM
.xlsx files BarrieM Windows XP 1 17th Oct 2007 04:52 PM


Features
 

Advertising
 

Newsgroups
 


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