VBA - Copy Worksheets with same Prefix in Name to new Workbook

Rahul79

New Member
Joined
Sep 3, 2009
Messages
25
Hello all,

I have a task at hand and am completely unsure on how to proceed. I was able to find a Macro online from "http://bredow.me/index.php/home/visual-basic-for-excel/vb-processes-and-snippets/split-worksheets-into-separate-files/" which is as below.

Here is my scenario. I have a workbook with over 200+ worksheets. So to categorize: 10 - Expense, 10 - FTE, 10 - WLU, 20 - Expense, 20 - FTE and so on.

I am trying to create a new Workbook for each prefix and to include all with those prefix. So, I would have a workbook saved as 10, with 3 worksheets 10 - Expense, 10 - FTE, 10 - WLU and then a Workbook saved as 20 with only 2 worksheets 20 - Expense, 20 - FTE, all saved in a specific location.

At the minimum, each workbook will have 1 worksheet (The Expense Worksheet). The FTE and WLU differ.

I had a Macro for that written several years ago, but then I ran in to KUTools and have been using that for several of my Automated Splits. This one is just tricky.

As I said, I honestly dont know where to start from. My assumption is:

Code:
  For Each sht In wbSource.Sheets
        sht.Copy
        Set wbDest = ActiveWorkbook


Here is the whole Macro:

Code:
Sub TabsToXlsxFiles()
 
    'Creates an individual workbook for each worksheet in the active workbook.
    Dim wbDest As Workbook
    Dim wbSource As Workbook
    Dim sht As Object 'Could be chart, worksheet, Excel 4.0 macro,etc.
    Dim strSavePath As String
    Dim filePrefix As String
    filePrefix = inputBox("Enter File Prefix if any desired (a space will separate the _
        prefix and the sheet name)", "File Prefix", "", 550, 550)
    fileSuffix = inputBox("Enter File Suffix if any desired (a space will separate the _
        sheet name from the suffix)", "File Suffix", "", 550, 550)
  
    On Error GoTo ErrorHandler
  
    Application.ScreenUpdating = False 'Don't show any screen movement
 
    strSavePath = "C:\Test\excelsplits\" 'Change this to suit your needs
 
    Set wbSource = ActiveWorkbook
  
    For Each sht In wbSource.Sheets
        sht.Copy
        Set wbDest = ActiveWorkbook
        wbDest.SaveAs strSavePath & filePrefix & " " & sht.Name & " " & fileSuffix
        wbDest.Close 'Remove this if you don't want each book closed after saving.
    Next
  
    Application.ScreenUpdating = True
  
Exit Sub
 
ErrorHandler: 'Just in case something bad happens
    MsgBox "An error has occurred. Error number=" & Err.Number & _
        ". Error description=" & Err.Description & "."
End Sub

Any help highly appreciated.

Thanks.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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