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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,270
Office Version
365
Platform
Windows
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:

Bill_k

New Member
Joined
Apr 22, 2015
Messages
10
Funny enough... it worked the first time but then gave this error:

ActiveWorkbook.Close savechanges:=True, Filename:=Pth & "" & Ky & ".xlsx"
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,270
Office Version
365
Platform
Windows
What error message did it give?
If the files already exist it should have asked if you want to replace it.
 

Bill_k

New Member
Joined
Apr 22, 2015
Messages
10
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,270
Office Version
365
Platform
Windows
Glad to help & thanks for the feedback
 

Forum statistics

Threads
1,082,126
Messages
5,363,320
Members
400,725
Latest member
excelingtolearn

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top