Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to save each worksheet as seperate file?

I am trying to make a simple macro function that will save ALL
worksheets in one spreadsheet file into a separate spreadsheet file.
(1 file for each worksheet.)

For example, if "MASTER.xls" has 50 worksheets, then the macro would
create the following:

worksheet1.xls
worksheet2.xls
..
..
worksheet50.xls

(The name of the file should be the name of the worksheet.)

I have created the following code, but it does not work. It does
create the first spreadsheet file correctly, but then it gives me the
following error:

Run-time error ‘9': Subscript out of range

When I do the debug, the yellow highlight is on for the
"Sheets(Sheet.name).Select" line.

I'm not sure what is wrong, because the MsgBox alert shows the correct
worksheet name...

Here is my current code:

Sub Macro_Svae()
'
' This macro will create a new workbook (spreadsheet file)
' for every sheet in the current workbook.
'
Dim worksheet_filepath As String
Dim worksheet_filename As String
Dim Sheet As Worksheet

worksheet_filepath = "C:\workfiles\andy"

' Do this for each worksheet in the workbook.
For Each Sheet In Worksheets

worksheet_filename = Sheet.name
MsgBox Sheet.name

Sheets(Sheet.name).Select
Sheets(Sheet.name).Copy

'ChDir worksheet_filepath
ActiveWorkbook.SaveAs FILENAME:= _
worksheet_filepath & "\" & worksheet_filename & ".xls", _
FileFormat:=xlNormal, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False

'Dim Sheet As Worksheet

Next Sheet

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default How to save each worksheet as seperate file?

Try this one
Change the path yours

Sub test()
Dim a As Integer
Application.ScreenUpdating = False
For a = 1 To ThisWorkbook.Worksheets.Count
ThisWorkbook.Sheets(a).Copy
ActiveWorkbook.SaveAs "C:\" & Sheets(1).Name & ".xls"
ActiveWorkbook.Close False
Next a
Application.ScreenUpdating = True
End Sub


--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2000-2003)
www.rondebruin.nl



"Henri" wrote in message om...
I am trying to make a simple macro function that will save ALL
worksheets in one spreadsheet file into a separate spreadsheet file.
(1 file for each worksheet.)

For example, if "MASTER.xls" has 50 worksheets, then the macro would
create the following:

worksheet1.xls
worksheet2.xls
.
.
worksheet50.xls

(The name of the file should be the name of the worksheet.)

I have created the following code, but it does not work. It does
create the first spreadsheet file correctly, but then it gives me the
following error:

Run-time error '9': Subscript out of range

When I do the debug, the yellow highlight is on for the
"Sheets(Sheet.name).Select" line.

I'm not sure what is wrong, because the MsgBox alert shows the correct
worksheet name...

Here is my current code:

Sub Macro_Svae()
'
' This macro will create a new workbook (spreadsheet file)
' for every sheet in the current workbook.
'
Dim worksheet_filepath As String
Dim worksheet_filename As String
Dim Sheet As Worksheet

worksheet_filepath = "C:\workfiles\andy"

' Do this for each worksheet in the workbook.
For Each Sheet In Worksheets

worksheet_filename = Sheet.name
MsgBox Sheet.name

Sheets(Sheet.name).Select
Sheets(Sheet.name).Copy

'ChDir worksheet_filepath
ActiveWorkbook.SaveAs FILENAME:= _
worksheet_filepath & "\" & worksheet_filename & ".xls", _
FileFormat:=xlNormal, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False

'Dim Sheet As Worksheet

Next Sheet

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default How to save each worksheet as seperate file?

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
Save a worksheet by itself to a new file Rich Excel Discussion (Misc queries) 2 December 9th 08 03:01 PM
How do I have seperate headers for seperate pages in 1 worksheet? JessicaDL Excel Discussion (Misc queries) 1 February 22nd 08 07:14 PM
how to save a single column as a seperate file grc46 Excel Discussion (Misc queries) 2 May 20th 07 07:35 PM
Save worksheet to seperate file? George Excel Discussion (Misc queries) 4 October 30th 06 08:30 PM
Calling a specific worksheet from a hyperlink in a seperate file. TheChris Excel Worksheet Functions 0 February 2nd 06 01:48 AM


All times are GMT +1. The time now is 06:44 PM.

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"