Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Need to Export All Worksheets from All Workbooks in Folder to Separate FIles not as CSV Files

I posted earlier about this script (http://tinyurl.com/ydbo3c)
searching for a way to select which file format to export to and Bob
Phillips kindly helped out. Unfortunately, if you choose an Excel
format it just save multiple copies of the original file instead of
exporting each file separately as an xls file. So I'll start over.

I want to use this Dave Peterson script (http://tinyurl.com/yd9dp3)
below to export all worksheets in all workbooks in a folder to separate
xls files. The script works as configured for csv files, but if you
change the line FileFormat:=xlCSV to xlWorkbookNormal or xlExcel9795
(seehttp://msdn2.microsoft.com/en-us/library/microsoft.office.interop.excel.xlfileformat.aspx),
the script doesn't work but saves multiple copies of each original xls
file with all the worksheets intact in each file.

Option Explicit
Private Sub CommandButton1_Click()

Dim myfiles() As String
Dim i As Integer
Dim myfile As String
Dim myfolder As String
Dim strpath As String
Dim strfilename As String

Dim wks As Worksheet

myfolder = InputBox("Enter complete path to the Excel files you wish to
convert to CSV format. Put an \ on the end of the path.", "Excel File
Folder Path")

With Application.FileSearch
.NewSearch
.LookIn = myfolder
.SearchSubFolders = True
.Filename = "*.xls"
If .Execute() 0 Then
ReDim Preserve myfiles(1 To .FoundFiles.Count)
Application.StatusBar = "Found Files: " &
..FoundFiles.Count
For i = 1 To .FoundFiles.Count
myfiles(i) = .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
Exit Sub
End If

End With

For i = LBound(myfiles) To UBound(myfiles)
Application.StatusBar = "Processing #" & i & ": " & myfiles(i)

Workbooks.Open Filename:=myfiles(i), ReadOnly:=True,
UpdateLinks:=False

For Each wks In ActiveWorkbook.Worksheets
wks.Activate
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs _
Filename:=Left(myfiles(i), Len(myfiles(i)) - 4) & "_" _
& wks.Name, _
FileFormat:=xlCSV
Application.DisplayAlerts = True
Next wks

ActiveWorkbook.Close savechanges:=False

Next i

Application.StatusBar = False

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Need to Export All Worksheets from All Workbooks in Folder to Separate FIles not as CSV Files

See this example for one file
http://www.rondebruin.nl/copy6.htm

You can use the code from this macro in a loop
See my site for code to loop through all files in a folder
http://www.rondebruin.nl/tips.htm


--
Regards Ron de Bruin
http://www.rondebruin.nl



"socrtwo" wrote in message ps.com...
I posted earlier about this script (http://tinyurl.com/ydbo3c)
searching for a way to select which file format to export to and Bob
Phillips kindly helped out. Unfortunately, if you choose an Excel
format it just save multiple copies of the original file instead of
exporting each file separately as an xls file. So I'll start over.

I want to use this Dave Peterson script (http://tinyurl.com/yd9dp3)
below to export all worksheets in all workbooks in a folder to separate
xls files. The script works as configured for csv files, but if you
change the line FileFormat:=xlCSV to xlWorkbookNormal or xlExcel9795
(seehttp://msdn2.microsoft.com/en-us/library/microsoft.office.interop.excel.xlfileformat.aspx),
the script doesn't work but saves multiple copies of each original xls
file with all the worksheets intact in each file.

Option Explicit
Private Sub CommandButton1_Click()

Dim myfiles() As String
Dim i As Integer
Dim myfile As String
Dim myfolder As String
Dim strpath As String
Dim strfilename As String

Dim wks As Worksheet

myfolder = InputBox("Enter complete path to the Excel files you wish to
convert to CSV format. Put an \ on the end of the path.", "Excel File
Folder Path")

With Application.FileSearch
.NewSearch
.LookIn = myfolder
.SearchSubFolders = True
.Filename = "*.xls"
If .Execute() 0 Then
ReDim Preserve myfiles(1 To .FoundFiles.Count)
Application.StatusBar = "Found Files: " &
.FoundFiles.Count
For i = 1 To .FoundFiles.Count
myfiles(i) = .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
Exit Sub
End If

End With

For i = LBound(myfiles) To UBound(myfiles)
Application.StatusBar = "Processing #" & i & ": " & myfiles(i)

Workbooks.Open Filename:=myfiles(i), ReadOnly:=True,
UpdateLinks:=False

For Each wks In ActiveWorkbook.Worksheets
wks.Activate
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs _
Filename:=Left(myfiles(i), Len(myfiles(i)) - 4) & "_" _
& wks.Name, _
FileFormat:=xlCSV
Application.DisplayAlerts = True
Next wks

ActiveWorkbook.Close savechanges:=False

Next i

Application.StatusBar = False

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Need to Export All Worksheets from All Workbooks in Folder to Separate FIles not as CSV Files


Ron de Bruin wrote:
See this example for one file
http://www.rondebruin.nl/copy6.htm

You can use the code from this macro in a loop
See my site for code to loop through all files in a folder
http://www.rondebruin.nl/tips.htm


--
Regards Ron de Bruin
http://www.rondebruin.nl


Mr de Bruin. Thanks for taking an interest and replying. Your Website
is great. I think I've got everything working the way I need.

SIncerely Paul Pruitt (socrtwo)

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
Export to XML from different worksheets (to different files) cfmartin76 Excel Worksheet Functions 0 January 26th 09 11:15 PM
Macro help - Moving 2 cells from 100 separate files into new folder Steven Excel Discussion (Misc queries) 7 July 10th 07 07:47 PM
how to export worksheets into seperate files PeterRad Excel Discussion (Misc queries) 2 August 9th 06 04:10 AM
Attach all Open Workbooks to email as separate files? nbaj2k[_25_] Excel Programming 3 August 3rd 06 02:25 PM
Export html page to Excel - support files folder Export html page to Excel - support file Excel Discussion (Misc queries) 1 June 24th 05 12:36 PM


All times are GMT +1. The time now is 02:30 AM.

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

About Us

"It's about Microsoft Excel"