Splitting excel sheets (Not copy paste data range) into separate workbooks with a certain name

Bill_k

New Member
Joined
Apr 22, 2015
Messages
10
I have a workbook with multiple sheets that have a name in column AA3 which might match in 2 or more sheets. The macro needs to search these cells and club all sheets with a similar name in AA3 and then make a new workbook out of them using the same name.

Please advise a solution.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hia
Is this what your after
Code:
Sub ExportShts()

    Dim Pth As String
    Dim Arr As Variant
    Dim Sht As Worksheet
    Dim ShtDict As Object
    Dim Rng As Range
    Dim Ky As Variant

Application.ScreenUpdating = False

    Pth = ThisWorkbook.Path
    Set ShtDict = CreateObject("scripting.dictionary")
    
    For Each Sht In Worksheets
        Set Rng = Sht.Range("AA3")
        With ShtDict
            If Not .Exists(Rng.Text) Then
                .Add Rng.Text, Sht.Name
            Else:
                .Item(Rng.Text) = .Item(Rng.Text) & "|" & Sht.Name
            End If
        End With
    Next Sht
    
    For Each Ky In ShtDict.Keys
        If InStr(ShtDict(Ky), "|") > 0 Then
        Arr = Split(ShtDict(Ky), "|")
        Sheets(Arr).Copy
        ActiveWorkbook.Close savechanges:=True, Filename:=Pth & "\" & Ky & ".[COLOR=#0000ff]xlsx[/COLOR]"
        End If
    Next Ky
    
Application.ScreenUpdating = True

End Sub
Change the file extension to suit
 
Last edited:
Upvote 0
Funny enough... it worked the first time but then gave this error:

ActiveWorkbook.Close savechanges:=True, Filename:=Pth & "" & Ky & ".xlsx"
 
Upvote 0
What error message did it give?
If the files already exist it should have asked if you want to replace it.
 
Upvote 0
I'm sorry that was because of a tweak I did to the name. Got it back to where it was. Working flawlessly now. Thank you very much.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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