Export Multiple Worksheets from a Workbook based on criteria

warleque

New Member
Joined
Aug 16, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I am trying to figure out a way to automate the holding of the control key and clicking on worksheets and then saving them as a new sheet.

I have a setup 'tab' that contains the sheet names (one tab for each location) along with the regional director for that location. I'd like to be able to copy each location sheet for the specific regional director into a new separate workbook.

In example sheet in the picture contains 7 location sheets and 3 regional director names. Each file name for the new file created can be the name of the regional director. The screen shot is from the setup 'tab'

Any help would be appreciated...
 

Attachments

  • EXCEL_Example.jpg
    EXCEL_Example.jpg
    56.3 KB · Views: 6

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi warleque,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    'https://www.mrexcel.com/board/threads/export-multiple-worksheets-from-a-workbook-based-on-criteria.1267107

    Dim varItem As Variant, varItems As Variant
    Dim strArrSheets() As String
    Dim wsSrc As Worksheet
    Dim strSaveDir As String
    Dim lngLastRow As Long
    Dim intArrayIndex As Integer, intWBCount As Integer
    Dim rngCell As Range
   
    Application.ScreenUpdating = False
   
    Set wsSrc = ThisWorkbook.Sheets("SetUp")
    strSaveDir = "D:\Robert\MS_Office\Excel" 'This is the path where the workbooks will be saved. Change to suit.
    strSaveDir = IIf(Right(strSaveDir, 1) <> "\", strSaveDir & "\", strSaveDir) 'Ensure the path where the workbooks are to be saved to have a trailing backslash '\'
    lngLastRow = wsSrc.Cells(Rows.Count, "E").End(xlUp).Row   
    varItems = Evaluate("UNIQUE('" & wsSrc.Name & "'!E3:E" & lngLastRow & ")")

    For Each varItem In varItems
        For Each rngCell In wsSrc.Range("A3:A" & lngLastRow)
            If StrConv(rngCell.Offset(0, 4), vbLowerCase) = StrConv(varItem, vbLowerCase) Then
                intArrayIndex = intArrayIndex + 1
                ReDim Preserve strArrSheets(1 To intArrayIndex)
                strArrSheets(intArrayIndex) = rngCell
            End If
        Next rngCell
        If intArrayIndex >= 1 Then
            ThisWorkbook.Sheets(strArrSheets).Copy
            ActiveWorkbook.SaveAs strSaveDir & CStr(varItem) & ".xlsx", FileFormat:=51 'Workbook default
            ActiveWorkbook.Close SaveChanges:=False
            intWBCount = intWBCount + 1
        End If
        intArrayIndex = 0: Erase strArrSheets
    Next varItem
   
    Application.ScreenUpdating = False
   
    Select Case intWBCount
        Case 0
            MsgBox "There were no workbooks created." & vbNewLine & "Check the data layout and code and try again.", vbExclamation
        Case Else
            MsgBox "There were " & Format(intWBCount, "#,##0") & " workbooks created and saved in the..." & vbNewLine & strSaveDir & vbNewLine & "...folder ready for review.", vbInformation
    End Select

End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,702
Members
452,667
Latest member
vanessavalentino83

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