save individual worksheets in a workbook

L

Legacy 15162

Guest
I would like to save individual worksheets in a workbook as individual files.....this is how i was beginning to code
Public Function PDFCreate()

Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim stWkshtName As String

On Error GoTo pdferror

Set xlApp = GetObject(, "Excel.Application")

Set xlWB = xlApp.Workbooks.Open("\\test\testing\testfolder\periodtest\@test.xls")

For i = 1 To xlWB.Worksheets.Count()
Set xlWS = xlWB.Worksheets(i)

stWkshtName = xlWS.Name

If (Left(stWkshtName, 1)) = "0" Then
xlwb.SaveAs(

End If

next i

i would like to have the destination folder be hardcoded so that no user can change.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
Will this work for you?:
Code:
Sub SplitWorkbook()
    Dim ws As Worksheet
    Dim DisplayStatusBar As Boolean
    
    DisplayStatusBar = Application.DisplayStatusBar
    Application.DisplayStatusBar = True
    Application.ScreenUpdating = False
    
    For Each ws In ThisWorkbook.Sheets
        Dim NewFileName As String
        Application.StatusBar = ThisWorkbook.Sheets.Count & " Remaining Sheets"
        If ThisWorkbook.Sheets.Count <> 1 Then
        ' Instead of ThisWorkbook.Path, you can hardcode a path
            NewFileName = ThisWorkbook.Path & "\" & ws.Name & ".xls"
            ws.Copy
            ActiveWorkbook.Sheets(1).Name = "Sheet1"
            ActiveWorkbook.SaveAs Filename:=NewFileName
            ActiveWorkbook.Close savechanges:=False
        Else
            NewFileName = ThisWorkbook.Path & "\" & ws.Name & ".xls"
            ws.Name = "Sheet1"
            ThisWorkbook.SaveAs Filename:=NewFileName
        End If
    Next
    
    Application.StatusBar = False
    Application.DisplayStatusBar = DisplayStatusBar
    Application.ScreenUpdating = True
    
End Sub

Hope that helps!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,193
Messages
5,768,772
Members
425,492
Latest member
blueexcel123

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
Top