Help with VBA Code Split Worksheets

lisaming

New Member
Joined
Nov 7, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a working VBA code however, I am trying to change it for "ThisWorkBook" to where I can choose any file and run the macro to split the worksheets into separate files.

Pull this from another macro:
Private Sub Workbook_Open()
fnameList = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", Title:="Choose Excel files to merge", MultiSelect:=False)

Existing 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
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Perhaps something like this.
VBA Code:
Sub SplitEachWorksheet()
Dim wb As Workbook
Dim fnameList As String
Dim FPath As String

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    fnameList = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", Title:="Choose Excel file to split", MultiSelect:=False)

    Set wb = Workbooks.Open(fnameList
    FPath = wb.Path

    For Each ws In wb.Sheets
        ws.Copy
        Application.ActiveWorkbook.SaveAs Filename:=FPath & "\" & ws.Name & ".xlsx"
        Application.ActiveWorkbook.Close False
    Next

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Perhaps something like this.
VBA Code:
Sub SplitEachWorksheet()
Dim wb As Workbook
Dim fnameList As String
Dim FPath As String

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    fnameList = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", Title:="Choose Excel file to split", MultiSelect:=False)

    Set wb = Workbooks.Open(fnameList
    FPath = wb.Path

    For Each ws In wb.Sheets
        ws.Copy
        Application.ActiveWorkbook.SaveAs Filename:=FPath & "\" & ws.Name & ".xlsx"
        Application.ActiveWorkbook.Close False
    Next

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

Thanks for the feedback, however I tried it and it didnt work. How do i incorporate the " Private Sub Workbook_Open() " code into the current one so that I can select the file that needs to be split?
 
Upvote 0
How did it not work?

Did yo fix the typo?
 
Upvote 0
How do i incorporate the " Private Sub Workbook_Open() " code into the current one so that I can select the file that needs to be split?

This is a confusing question, because it does not coincide with this statement.
I am trying to change it for "ThisWorkBook" to where I can choose any file and run the macro to split the worksheets into separate files.

Can you be a little more explicit in what you want to do. Pretend you have no code at all and then explain what you want.
 
Upvote 0
This is a confusing question, because it does not coincide with this statement.


Can you be a little more explicit in what you want to do. Pretend you have no code at all and then explain what you want.
Sure thing, I need a macro that will split any workbook (with multiple worksheets) into separate files. The macro will need to prompt the User to select the file to split; and save the separate worksheets in the same location as where the original file was selected from.

Hope this helps clarify.
 
Upvote 0
After adding the right parenthesis to the Workbooks.Open statement, @Norie code ran without error and produced desired results for me. I ran the code from code module1 of my workbook. It opens the GetFilename dialo9g box, where I selected a file name and clicked 'Open'. The code then opened the selected workbook, copied the sheets to produce new workbooks, saving each one to the original directory and closing them in turn. What parrt of this process is not working for you.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,519
Members
448,968
Latest member
Ajax40

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