Forum Discussion

Hussein_Mohamed's avatar
Hussein_Mohamed
Brass Contributor
Nov 08, 2023
Solved

Excel Help

Dears, is it applicable to do the below with excel: if i have an excel master data updated on a daily basis and the file including alot of projects, is there any way to seperate the sheets with pro...
  • NikolinoDE's avatar
    NikolinoDE
    Nov 12, 2023

    Hussein_Mohamed 

    You can achieve this with a VBA (Visual Basic for Applications) macro in Excel. The following code saves each sheet as an individual workbook in a defined location with the sheet name and the current date appended to the filename. In the example, you will find an approach.

    This macro will run when the workbook is closed:

     

    Open the Visual Basic for Applications editor by pressing Alt + F11, and then insert this code in the workbook's module.

    Vba code (is untested):

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Dim ws As Worksheet
        Dim savePath As String
        Dim wb As Workbook
        Dim fileName As String
        Dim currentDate As String
    
        ' Define the folder path where you want to save the separated workbooks
        savePath = "C:\Your\Folder\Path\" ' Update with your desired path
    
        ' Create the folder if it doesn't exist
        If Dir(savePath, vbDirectory) = "" Then
            MkDir savePath
        End If
    
        For Each ws In ThisWorkbook.Worksheets
            ' Create a new workbook for each worksheet
            Set wb = Workbooks.Add
            ws.Copy Before:=wb.Sheets(1)
            
            ' Get the current date to append to the filename
            currentDate = Format(Now(), "yyyy-mm-dd")
            
            ' Form the filename with sheet name and date
            fileName = savePath & ws.Name & "_" & currentDate & ".xlsx"
            
            ' Save the new workbook with the formatted name and close it
            wb.SaveAs fileName
            wb.Close SaveChanges:=False
        Next ws
    End Sub

    This code will trigger when the workbook is closed. It loops through each sheet, creates a new workbook, copies the sheet into that new workbook, and saves it in the defined location with the sheet name and the current date.

     

    Remember to change savePath = "C:\Your\Folder\Path\" to the path where you want to save the separate workbooks. This script will create a new file for each sheet upon closing the workbook.

    AI was used to support the text.

     

    VBA functionality is not available or possible in all Excel versions. It may also depend on where it is stored. However, some information was requested that you did not provide, so my help is unfortunately limited due to a lack of information.

     

    But still hope it helped you :smile:.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

    Was the answer useful? Mark as best response and Like it!

    This will help all forum participants.

Resources