Splitting worksheets in Excel with Senstivity Labels following master file using VBA

liclice

New Member
Joined
Nov 1, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am trying to split the worksheets in my excel files into different files saving under the worksheet names using the following code:


Sub SplitEachWorksheet()
Dim FPath As String
FPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Sheets
ws.Copy
Application.ActiveWorkbook.SaveAs Filename:=FPath & "\" & ws.Name & ".xlsx"
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

However, I cannot get the files saved as sensitivity label is required. Are there any ways to amend the above code in order to save the files based on the master file's label?

Thanks a lot.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi *liclice and Welcome to the Board! Here's a variation of @jolivanes code posted here...
VBA Code:
Sub SaveSheet()
Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
Sheets(sh.Name).Copy
    With ActiveWorkbook
            Application.DisplayAlerts = False
                .SaveAs ThisWorkbook.Path & "\" & sh.Name & ".xlsx", FileFormat:=51
            Application.DisplayAlerts = True
        .Close
    End With
Next sh
End Sub
HTH. Dave
ps. Please use code tags
 
Upvote 0
Hi *liclice and Welcome to the Board! Here's a variation of @jolivanes code posted here...
VBA Code:
Sub SaveSheet()
Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
Sheets(sh.Name).Copy
    With ActiveWorkbook
            Application.DisplayAlerts = False
                .SaveAs ThisWorkbook.Path & "\" & sh.Name & ".xlsx", FileFormat:=51
            Application.DisplayAlerts = True
        .Close
    End With
Next sh
End Sub
HTH. Dave
ps. Please use code tags
Thank you Dave but unfortunately I tried all of the codes they all don't work and give error messages.
 
Upvote 0
I trialed the code above, and re-trialed again before posting, and it works ok. What is the error message? Is this a onedrive file or stored on your pc? It probably won't work for a onedrive file. Dave
 
Upvote 0
I trialed the code above, and re-trialed again before posting, and it works ok. What is the error message? Is this a onedrive file or stored on your pc? It probably won't work for a onedrive file. Dave
Hello Dave, I was trying to store in a network drive.
 
Upvote 0
For a network drive, you will need to first save it to your pc (use SaveCopyAs), generate your new workbooks (with code above) and then use CopyFile to place the new wbs on the network. You can then delete the wbs on your pc. Perhaps there's an easier more direct route that others will share? Dave
 
Upvote 0

Forum statistics

Threads
1,224,809
Messages
6,181,076
Members
453,020
Latest member
mattg2448

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