loop for Exporting worksheets to a new workbook

rakesh933

New Member
Joined
Dec 12, 2013
Messages
8
Hi All,

i have a workbook with multiple worksheets, except for "Sheet1" i want all the other sheet to be moved as individual new workbooks (with same name as the worksheet i.e new workbook name = worksheet name + current date)

For ex : i have sheet named : sheet1, apple, samsung,HTC.

i need to move individual worksheets (except sheet1) to create individual workbooks

Please help.

Thanks
Rakesh
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Code:
'change this constant to the path you want to save to.  Make sure it ends with \

Public Const sPath As String = "C:\Temp\"


Sub CopySheetsToNewBook()
    Dim sSavePath As String
    Dim ws As Worksheet
    Dim wbCopy As Workbook
    Dim wsCopy As Worksheet
    Dim wbTo As Workbook
    Set wbCopy = ThisWorkbook
    Dim wsDel As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Sheet1" Then    'change the name of this sheet to exclude whatever sheet name you want
            sSavePath = sPath & ws.Name & Format(Date, "YYYYMMDD")
            Set wbTo = Workbooks.Add
            On Error Resume Next
            Set wsDel = wbTo.Sheets(ws.Name)
            On Error GoTo 0
            If Not wsDel Is Nothing Then wsDel.Delete
            Set wsDel = Nothing
            ws.Copy wbTo.Sheets(1)
            wbTo.SaveAs sSavePath
            wbTo.Close
        End If




    Next ws
End Sub
 
Upvote 0
Try this.
Code:
Dim wb As Workbook
Dim ws As Worksheet
Dim strPath As String

    strPath = ActiveWorkbook.Path

    For Each ws In ActiveWorkbook.Sheets
        If ws.Name<>"Sheet1" Then
            ws.Copy
            Set wbNew = ActiveWorkbook
            wbNew.SaveAs FileName:= strPath & Application.PathSeparator & ws.Name & Date & ".xlsx
            wbNew.Close False ' optional
        End If
    Next ws
 
Upvote 0
Try this.
Code:
Dim wb As Workbook
Dim ws As Worksheet
Dim strPath As String

    strPath = ActiveWorkbook.Path

    For Each ws In ActiveWorkbook.Sheets
        If ws.Name<>"Sheet1" Then
            ws.Copy
            Set wbNew = ActiveWorkbook
            wbNew.SaveAs FileName:= strPath & Application.PathSeparator & ws.Name & Date & ".xlsx
            wbNew.Close False ' optional
        End If
    Next ws


Thanks much works like a charm ! just one thing though , how to i change the path ? say i wanna save all the files to "C:\Temp" ?
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

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