View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
[email protected] mark.driscol@wal-mart.com is offline
external usenet poster
 
Posts: 6
Default Convert every xls file in a folder to csv

If you put an Excel file containing this macro in the same directory as
your other Excel files, you should be able to execute a macro like this
macro on the other files.

Option Explicit

Sub ConvertToCSV()
'
' Uses code from John Walkenbach's Power Programming book
'
Dim i As Long
Dim NumFiles As Long
Dim FileName As String
Dim FileNames() As String

' Get name of first file in backlog directory
FileName = Dir(ThisWorkbook.Path & "/*.xls")

NumFiles = 1
ReDim Preserve FileNames(1 To NumFiles)
FileNames(NumFiles) = FileName

' Get other file names, if any
Do While FileName < ""
FileName = Dir()
If FileName < "" Then
NumFiles = NumFiles + 1
ReDim Preserve FileNames(1 To NumFiles)
FileNames(NumFiles) = FileName
End If
Loop

' Save each file as a .csv file, overwriting any existing .csv
files
Application.DisplayAlerts = False
For i = 1 To UBound(FileNames)
If FileNames(i) < ThisWorkbook.Name Then
Workbooks.Open FileName:=FileNames(i)
ActiveWorkbook.SaveAs _
FileName:=Left(FileNames(i), Len(FileNames(i)) - 4) &
".csv", _
FileFormat:=xlCSV
ActiveWorkbook.Close
End If
Next i
Application.DisplayAlerts = True

End Sub


Mark