Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Convert every xls file in a folder to csv

I need some vb code that I could perhaps run from access that will look
in a particular folder I could specify in the code, and convert all the
files (just excel are saved there) to .csv. Is this possible? This
would be a huge help for me and any help would be greatly appreciated.


Also, there are only 5 files in the folder (the folder path never
changes) but they are updated monthly so if we cant evaluate the entire
population that is in the folder and have to write seperate code to
convert each file that is cool too.

Here is my process:

Excel Files Updated and saved to directory --- Saved as csv in same
directory--- Imported in Access --- Access Reports Runs ---Done

I would love to have all this done in a click of a button, im so close
I just need to get this conversion to csv piece.

Thanks.

  #2   Report Post  
Posted to microsoft.public.excel.programming
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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Convert every xls file in a folder to csv

I am getting an error at this part:

Workbooks.Open FileName:=FileNames(i)

the message is that it cant find the file and to make sure the
directory or name hasn't changed, it has the file name included in the
message. I think the code works to collect and store all the files, it
just is having problem going back to the list to open the files.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default Convert every xls file in a folder to csv

This is probably because it's looking in the wrong directory: the
FIleNames() array doesn't contain the full path, so VBA is trying
to find it in whatever it thinks is the current directory. Try

Workbooks.Open FileName:=ThisWorkbook.Path & "\" & FileNames(i)


JI wrote:
I am getting an error at this part:

Workbooks.Open FileName:=FileNames(i)

the message is that it cant find the file and to make sure the
directory or name hasn't changed, it has the file name included in the
message. I think the code works to collect and store all the files, it
just is having problem going back to the list to open the files.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Convert every xls file in a folder to csv

Andrew and MArk thanks everything works great now.


Andrew Taylor wrote:
This is probably because it's looking in the wrong directory: the
FIleNames() array doesn't contain the full path, so VBA is trying
to find it in whatever it thinks is the current directory. Try

Workbooks.Open FileName:=ThisWorkbook.Path & "\" & FileNames(i)


JI wrote:
I am getting an error at this part:

Workbooks.Open FileName:=FileNames(i)

the message is that it cant find the file and to make sure the
directory or name hasn't changed, it has the file name included in the
message. I think the code works to collect and store all the files, it
just is having problem going back to the list to open the files.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Convert every xls file in a folder to csv

Jon,
If the ultimate aim is to get the data in Access, can you not just link the
Excel files in Access ?

NickHK

"JI" wrote in message
oups.com...
I need some vb code that I could perhaps run from access that will look
in a particular folder I could specify in the code, and convert all the
files (just excel are saved there) to .csv. Is this possible? This
would be a huge help for me and any help would be greatly appreciated.


Also, there are only 5 files in the folder (the folder path never
changes) but they are updated monthly so if we cant evaluate the entire
population that is in the folder and have to write seperate code to
convert each file that is cool too.

Here is my process:

Excel Files Updated and saved to directory --- Saved as csv in same
directory--- Imported in Access --- Access Reports Runs ---Done

I would love to have all this done in a click of a button, im so close
I just need to get this conversion to csv piece.

Thanks.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need code to save file to new folder, erase from old folder Ron M. Excel Discussion (Misc queries) 1 February 24th 06 06:02 PM
open file from folder save in new folder tim64[_3_] Excel Programming 20 June 17th 05 07:58 PM
Create Folder and Text File in folder Todd Huttentsine Excel Programming 2 April 29th 04 03:41 PM
Move file to different folder Don Guillett[_4_] Excel Programming 4 August 29th 03 07:35 PM


All times are GMT +1. The time now is 03:37 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"