RE: MACRO to save worksheets as seperate files ?

stelmarkov

New Member
Joined
Jun 6, 2011
Messages
5
RE: MACRO to save worksheets as seperate files ?

Hello, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I've found in your archive the Macro code Posted by Dank on November 14, 2001 12:51 AM re how to Saves worksheets as separate files from one main workbook<o:p></o:p>
<o:p></o:p>
This code does save the worksheets as a separate files and that is exactly what I’ve been looking for however <o:p></o:p>
<o:p></o:p>
1) I don't need to save each worksheet from my master work book e.g I need to save only 20 separate files
<o:p> </o:p>
How do I amend the below code posted by Dank ? Can we specify in the code the range of the worksheets I need to save ? <o:p></o:p>
<o:p> </o:p>
Please note that I am a beginner and I found the below code a bit simple and understanding so is it possible to amend the below code to my need
PS. The Worksheets that I need to copy and distribute are named with different Doctors Surnames<o:p></o:p>
<o:p> </o:p>
2) I work in Excel 2007 but most of my master workbooks are saved in .xls and not in .xlsx as we send the files to external users and not every user have latest version of excel, Dank’s macro currently saves all files in .xlsx<o:p></o:p>
<o:p> </o:p>
I’ve tried to amend the following Save As macro to <o:p></o:p>
wbDest.SaveAs strSavePath & sht.Name & " " & Format(DateSerial(Year(Now), Month(Now) - 1, 1), "mmmm yy") & ".xls")
<o:p> </o:p>
The Marco saves the file with ext .xls BUT when I open the file, message says that” The file is saved in different format than specified in the file extension” Where in the macro can you specify the extension of the file then?<o:p></o:p>
<o:p> </o:p>
<o:p></o:p>I also tried to amend / add the following But I am getting the message “Object Variable or with block variable not set”<o:p></o:p>
<o:p> </o:p>
Dim strFile As String<o:p></o:p>
strFile = Dir(strSavePath & sht.Name & " " & Format(DateSerial(Year(Now), Month(Now) - 1, 1), "mmmm yy") & ".xls")<o:p></o:p>
wbDest.SaveAs strFile<o:p></o:p>
<o:p> </o:p>
<o:p>Thanking you in advance</o:p>
<o:p>Kind Regards,</o:p>
<o:p>Stal</o:p>
<o:p> </o:p>
Please refer to the Macro posted by Dank <o:p></o:p>
Sub CreateWorkbooks()
'Creates an individual workbook for each worksheet in the active workbook.
Dim wbDest As Workbook
Dim wbSource As Workbook
Dim sht As Object 'Could be chart, worksheet, Excel 4.0 macro,etc.
Dim strSavePath As String <o:p></o:p>

<o:p> </o:p>
On Error GoTo ErrorHandler<o:p></o:p>
<o:p></o:p>
Application.ScreenUpdating = False 'Don't show any screen movement <o:p></o:p>
strSavePath = "C:\Temp\" 'Change this to suit your needs <o:p></o:p>
<o:p> </o:p>
Set wbSource = ActiveWorkbook <o:p></o:p>
For Each sht In wbSource.Sheets
sht.Copy
Set wbDest = ActiveWorkbook
wbDest.SaveAs strSavePath & sht.Name
wbDest.Close 'Remove this if you don't want each book closed after saving.
Next <o:p></o:p>

Application.ScreenUpdating = True <o:p></o:p>
Exit Sub <o:p></o:p>
ErrorHandler: 'Just in case something hideous happens
MsgBox "An error has occurred. Error number=" & Err.Number & ". Error description=" & Err.Description & "."
End Sub
 
Re: MACRO to save worksheets as seperate files ?

Mark - thank you for your help! It work however the path that the workbooks were saved into are different from the folder that the original file was in. The macro saved the workbooks into:

C:\Documents and Settings\matt\Application Data\Microsoft\Excel\XLSTART

Could the fact that im on a company computer be affecting the path where excel defaults to?

thanks again!

Nikegeo,


Help of Excel

Application the ThisWorkbook property

Returns a Workbook object that represents the workbook where the current macro code is running.

Use this property to refer to the workbook that contains the macro code. ThisWorkbook is the only way to refer to an add-in workbook from inside the add-in.

Workbook Path property

Returns a String value that represents the full path to the application, excluding the final separator and name of the application.

Make sure that the macro is actually stored in the master workbook.

The following command sets the location where Excel must save the new workbook (same path of the master workbook - were is the macro)

'Store the path of the master workbook
'in to the variable strSavePath
strSavePath = ThisWorkbook.Path & "\"

Markmzz
 
Last edited:
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Re: MACRO to save worksheets as seperate files ?

Mark - thanks for the quick reply - I saved the Macro in the workbook, not the personal, and it works perfectly!

Thank You so much for all your help

-Matt
 
Upvote 0
Re: MACRO to save worksheets as seperate files ?

Mark - thanks for the quick reply - I saved the Macro in the workbook, not the personal, and it works perfectly!

Thank You so much for all your help

-Matt

Matt,

You are welcome and thanks for the feedback.

Markmzz
 
Upvote 0

Forum statistics

Threads
1,225,157
Messages
6,183,248
Members
453,152
Latest member
ChrisMd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top