Forum Discussion
Excel Help
- Nov 12, 2023
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 SubThis 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
.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.
What exactly do you want to accomplish?
i want to separate the workbook into sheets to be saved with sheet name and date of separation when i save and close the worksheet in defined location
Questions about master sheet, what is meant when the master sheet is closed?
Is it workbooks or worksheets?
i mean the workbook
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 SubThis 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
.
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.
- vemulnxNov 20, 2023Copper Contributor
Hi, It is very close to what I am looking for. Is there any way we can save each sheet as a individual sheet in the same workbook.
- NikolinoDENov 20, 2023Platinum Contributor
If you want to save each sheet as an individual sheet in the same workbook instead of creating separate workbooks, you can modify the code accordingly.
Here is an updated version of the code:
Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim ws As Worksheet Dim savePath As String Dim wb As Workbook Dim currentDate As String ' Define the folder path where you want to save the separated sheets 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 ' Loop through each worksheet in the workbook For Each ws In ThisWorkbook.Worksheets ' Create a new workbook Set wb = Workbooks.Add ' Copy the current worksheet to the new workbook ws.Copy Before:=wb.Sheets(1) ' Get the current date to append to the filename currentDate = Format(Now(), "yyyy-mm-dd") ' Save the new workbook with the formatted name and close it wb.SaveAs savePath & ws.Name & "_" & currentDate & ".xlsx" wb.Close SaveChanges:=False Next ws End SubThe code is untested, please backup your file first.
This modified code will create a new workbook for each sheet in the same workbook, copy the sheet into the new workbook, and save it with a filename based on the sheet name and the current date. Adjust the savePath variable to your desired folder path.
- Hussein_MohamedNov 12, 2023Brass Contributorthanks alot it works well with me, i have added time to the mentioned code
- NikolinoDENov 12, 2023Platinum ContributorThank you for your feedback.
I am pleased that you have found a solution and are moving forward with your project.
I also wish you much success!