Consulting

Results 1 to 10 of 10

Thread: it is possible to save individual sheets as a separate file?

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    40
    Location

    Question it is possible to save individual sheets as a separate file?

    I have 12 sheets each of departments and sub-departments.
    I want to save them as a separate file for monthly report.

    sample:

    january 2018 and so on...

    and the original will blank for the next month's report

    thank you very much in advance.

    god bless

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Didn't understand some of what you wanted, but this should be a start

    It puts the output workbooks in the same folder, and give them the previous month's as a file name




    Option Explicit
    Sub SplitWorkbook()
        Dim sDate As String
        Dim ws As Worksheet
        Dim wb1 As Workbook, wb2 As Workbook
        Dim sPath1 As String, sFile2 As String
        
        Application.ScreenUpdating = False
        
        Set wb1 = ThisWorkbook
        
        If Month(Now) - 1 = 0 Then
            sDate = Format(DateSerial(Year(Now) - 1, 12, 1), "mmmm yyyy")
        Else
            sDate = Format(DateSerial(Year(Now), Month(Now) - 1, 1), "mmmm yyyy")
        End If
            
            
        sPath1 = wb1.Path & Application.PathSeparator
            
        For Each ws In wb1.Worksheets
            sFile2 = sDate & " " & ws.Name
            ws.Copy
            Set wb2 = ActiveWorkbook
            
            On Error Resume Next
            Kill sPath1 & sFile2 & ".xlsx"
            On Error GoTo 0
            
            wb2.SaveAs Filename:=sPath1 & sFile2, FileFormat:=xlOpenXMLWorkbook
        
            wb2.Close (False)
        
            wb1.Activate
        
        Next
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Posts
    40
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Didn't understand some of what you wanted, but this should be a start

    It puts the output workbooks in the same folder, and give them the previous month's as a file name




    Option Explicit
    Sub SplitWorkbook()
        Dim sDate As String
        Dim ws As Worksheet
        Dim wb1 As Workbook, wb2 As Workbook
        Dim sPath1 As String, sFile2 As String
        
        Application.ScreenUpdating = False
        
        Set wb1 = ThisWorkbook
        
        If Month(Now) - 1 = 0 Then
            sDate = Format(DateSerial(Year(Now) - 1, 12, 1), "mmmm yyyy")
        Else
            sDate = Format(DateSerial(Year(Now), Month(Now) - 1, 1), "mmmm yyyy")
        End If
            
            
        sPath1 = wb1.Path & Application.PathSeparator
            
        For Each ws In wb1.Worksheets
            sFile2 = sDate & " " & ws.Name
            ws.Copy
            Set wb2 = ActiveWorkbook
            
            On Error Resume Next
            Kill sPath1 & sFile2 & ".xlsx"
            On Error GoTo 0
            
            wb2.SaveAs Filename:=sPath1 & sFile2, FileFormat:=xlOpenXMLWorkbook
        
            wb2.Close (False)
        
            wb1.Activate
        
        Next
        Application.ScreenUpdating = True
    End Sub





    I attached the file.

    thank you very much.


    how about the next month and so on?

    can I call it with the command button?

    I have a useform inputs data and click the corresponding button to send it to the corresponding sheets
    and save it as other files the original workbook will be clear again for the next month's computation.

    and the individually save sheets will serve as reports and months computation.
    Attached Files Attached Files

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Do you mean you want the sheets COLD STORAGE, FINANCE, ..., WAREHOUSE each copied to a separate workbook and then saved with a name like January 2018 COLD STORAGE.xlsx?

    The first macro generates a name from the previous month and the sheet name

    Since this is February, the names start with January 2018 ....

    If you don't want that, how do you want the time stamp generated, or do you want to enter it?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Regular
    Joined
    Jan 2018
    Posts
    40
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Do you mean you want the sheets COLD STORAGE, FINANCE, ..., WAREHOUSE each copied to a separate workbook and then saved with a name like January 2018 COLD STORAGE.xlsx?

    The first macro generates a name from the previous month and the sheet name

    Since this is February, the names start with January 2018...

    If you don't want that, how do you want the time stamp generated, or do you want to enter it?

    yes! exactly.

    its ok not to start in January. the start febuary2018 will be just very fine

    thank you very much ))

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    OK -- try this


    Option Explicit
    Sub SplitWorkbook()
        Dim sDate As String, sPrefix As String
        Dim wb1 As Workbook, wb2 As Workbook
        Dim sPath1 As String, sFile2 As String
        Dim i As Long
        Dim aSheetsToCopy As Variant, v As Variant
        
        'get default prefix -- Previous month + Year
        If Month(Now) - 1 = 0 Then
            sDate = Format(DateSerial(Year(Now) - 1, 12, 1), "mmmm yyyy")
        Else
            sDate = Format(DateSerial(Year(Now), Month(Now) - 1, 1), "mmmm yyyy")
        End If
        
        'see if the user wants to change it
        sPrefix = Application.InputBox("Enter the Prefix for the created workbooks, (blank) to exit", "Split Workbook", sDate, , , , , 2)
        
        'trim and if len = 0 then exit sub
        sPrefix = Trim(sPrefix)
        If Len(sPrefix) = 0 Then Exit Sub
        
        
        Application.ScreenUpdating = False
        
        'build list of sheets to extract
        aSheetsToCopy = Array("MAINTENANCE", "PRODUCTION", "PURCHASING", "QC", "SALES", "SHOWROOM", "WAREHOUSE", "COLD STORAGE", "FINANCE", "HR", "LADIES")
        
        'remember this workbook and path so we don't get confused
        Set wb1 = ThisWorkbook
        sPath1 = wb1.Path & Application.PathSeparator
        
        For Each v In aSheetsToCopy
        
            'make sure the sheet exists
            i = -1
            On Error Resume Next
            i = wb1.Worksheets(v).Index
            On Error GoTo 0
        
            If i = -1 Then
                Call MsgBox("Worksheet " & v & " does not exist", vbCritical + vbOKOnly, "Split Workbook")
                
            Else
                Application.StatusBar = "Copying " & v
                sFile2 = sPrefix & " " & v
                Worksheets(v).Copy
                Set wb2 = ActiveWorkbook
                
                On Error Resume Next
                Kill sPath1 & sFile2 & ".xlsx"
                On Error GoTo 0
                
                wb2.SaveAs Filename:=sPath1 & sFile2, FileFormat:=xlOpenXMLWorkbook
                
                wb2.Close (False)
                
                wb1.Activate
            End If
        Next
        
        Application.StatusBar = False
        Application.ScreenUpdating = True
        
        Call MsgBox("Worksheets have been copied to seperate workbooks", vbInformation + vbOKOnly, "Split Workbook")
        
        
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Regular
    Joined
    Jan 2018
    Posts
    40
    Location
    Quote Originally Posted by Paul_Hossler View Post
    OK -- try this


    Option Explicit
    Sub SplitWorkbook()
        Dim sDate As String, sPrefix As String
        Dim wb1 As Workbook, wb2 As Workbook
        Dim sPath1 As String, sFile2 As String
        Dim i As Long
        Dim aSheetsToCopy As Variant, v As Variant
        
        'get default prefix -- Previous month + Year
        If Month(Now) - 1 = 0 Then
            sDate = Format(DateSerial(Year(Now) - 1, 12, 1), "mmmm yyyy")
        Else
            sDate = Format(DateSerial(Year(Now), Month(Now) - 1, 1), "mmmm yyyy")
        End If
        
        'see if the user wants to change it
        sPrefix = Application.InputBox("Enter the Prefix for the created workbooks, (blank) to exit", "Split Workbook", sDate, , , , , 2)
        
        'trim and if len = 0 then exit sub
        sPrefix = Trim(sPrefix)
        If Len(sPrefix) = 0 Then Exit Sub
        
        
        Application.ScreenUpdating = False
        
        'build list of sheets to extract
        aSheetsToCopy = Array("MAINTENANCE", "PRODUCTION", "PURCHASING", "QC", "SALES", "SHOWROOM", "WAREHOUSE", "COLD STORAGE", "FINANCE", "HR", "LADIES")
        
        'remember this workbook and path so we don't get confused
        Set wb1 = ThisWorkbook
        sPath1 = wb1.Path & Application.PathSeparator
        
        For Each v In aSheetsToCopy
        
            'make sure the sheet exists
            i = -1
            On Error Resume Next
            i = wb1.Worksheets(v).Index
            On Error GoTo 0
        
            If i = -1 Then
                Call MsgBox("Worksheet " & v & " does not exist", vbCritical + vbOKOnly, "Split Workbook")
                
            Else
                Application.StatusBar = "Copying " & v
                sFile2 = sPrefix & " " & v
                Worksheets(v).Copy
                Set wb2 = ActiveWorkbook
                
                On Error Resume Next
                Kill sPath1 & sFile2 & ".xlsx"
                On Error GoTo 0
                
                wb2.SaveAs Filename:=sPath1 & sFile2, FileFormat:=xlOpenXMLWorkbook
                
                wb2.Close (False)
                
                wb1.Activate
            End If
        Next
        
        Application.StatusBar = False
        Application.ScreenUpdating = True
        
        Call MsgBox("Worksheets have been copied to seperate workbooks", vbInformation + vbOKOnly, "Split Workbook")
        
        
    End Sub


    Can I call this in command button?
    thank you very much, Paul

  8. #8
    VBAX Regular
    Joined
    Jan 2018
    Posts
    40
    Location
    Quote Originally Posted by Paul_Hossler View Post
    OK -- try this


    Option Explicit
    Sub SplitWorkbook()
        Dim sDate As String, sPrefix As String
        Dim wb1 As Workbook, wb2 As Workbook
        Dim sPath1 As String, sFile2 As String
        Dim i As Long
        Dim aSheetsToCopy As Variant, v As Variant
        
        'get default prefix -- Previous month + Year
        If Month(Now) - 1 = 0 Then
            sDate = Format(DateSerial(Year(Now) - 1, 12, 1), "mmmm yyyy")
        Else
            sDate = Format(DateSerial(Year(Now), Month(Now) - 1, 1), "mmmm yyyy")
        End If
        
        'see if the user wants to change it
        sPrefix = Application.InputBox("Enter the Prefix for the created workbooks, (blank) to exit", "Split Workbook", sDate, , , , , 2)
        
        'trim and if len = 0 then exit sub
        sPrefix = Trim(sPrefix)
        If Len(sPrefix) = 0 Then Exit Sub
        
        
        Application.ScreenUpdating = False
        
        'build list of sheets to extract
        aSheetsToCopy = Array("MAINTENANCE", "PRODUCTION", "PURCHASING", "QC", "SALES", "SHOWROOM", "WAREHOUSE", "COLD STORAGE", "FINANCE", "HR", "LADIES")
        
        'remember this workbook and path so we don't get confused
        Set wb1 = ThisWorkbook
        sPath1 = wb1.Path & Application.PathSeparator
        
        For Each v In aSheetsToCopy
        
            'make sure the sheet exists
            i = -1
            On Error Resume Next
            i = wb1.Worksheets(v).Index
            On Error GoTo 0
        
            If i = -1 Then
                Call MsgBox("Worksheet " & v & " does not exist", vbCritical + vbOKOnly, "Split Workbook")
                
            Else
                Application.StatusBar = "Copying " & v
                sFile2 = sPrefix & " " & v
                Worksheets(v).Copy
                Set wb2 = ActiveWorkbook
                
                On Error Resume Next
                Kill sPath1 & sFile2 & ".xlsx"
                On Error GoTo 0
                
                wb2.SaveAs Filename:=sPath1 & sFile2, FileFormat:=xlOpenXMLWorkbook
                
                wb2.Close (False)
                
                wb1.Activate
            End If
        Next
        
        Application.StatusBar = False
        Application.ScreenUpdating = True
        
        Call MsgBox("Worksheets have been copied to seperate workbooks", vbInformation + vbOKOnly, "Split Workbook")
        
        
    End Sub



    how can I change file save location?

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Change this line

     sPath1 = wb1.Path & Application.PathSeparator

    to

     sPath1 = "C:\Users\MyName\Desktp\etc" & Application.PathSeparator
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Can I call this in command button?
    thank you very much, Paul
    Yes

    1. Use the Developer tab, and insert a control -- I'd use a Form Control

    Capture.JPG

    2. Draw a box

    3. Assign the macro to it
    Capture2.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •