export data from one worksheet into multiple workbooks based on criteria

Sian1

Board Regular
Joined
Nov 9, 2009
Messages
90
Hi, does anyone know how to export data from one worksheet to multiple workbooks based on the criteria.
for example below, the criteria are based on the column "Name" and "Level", and save as following;
output: copy the data set into separate worksheets based on the "Level" by "Name"
Workbooks nameWorksheets name
RobertLevel 1Level 2Level 3Level 4
JohnLevel 1Level 2Level 3
DavidLevel 1Level 4
WilliamLevel 1Level 3


Dataset
IDOrder dateNameLevelSalaryJob TimePrev Exp
1Nov-10-22RobertLevel 3$57,00098144
2Jan-13-23JohnLevel 2$40,2009836
3Jan-11-23RobertLevel 1$21,45098381
4Jan-12-23DavidLevel 4$21,90098190
5Nov-10-22WilliamLevel 1$45,00098138
6Dec-07-22JohnLevel 1$32,1009867
7Dec-20-22RobertLevel 4$36,00098114
8Nov-10-22WilliamLevel 3$21,900980
9Jan-11-23CharlesLevel 1$27,90098115
10Dec-27-22ChristopherLevel 1$24,00098244
11Dec-16-22DanielLevel 1$30,30098143
12Dec-13-22JohnLevel 3$28,3509826
13Nov-21-22RobertLevel 2$27,7509834
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
More details are required to help you.

Do the "Name" workbooks already exist? If so, in which folder and do the "Level" sheets already exist? If so, do they contain existing data? If so, should the exported data overwrite it or append to it?
 
Upvote 0
Hello. I made the following macro (SianCCodeFactory) and it requires the use of two subordinate macros I created. I used the data you provided and was able to save workbooks as you described (at least, I believe so). It makes use of Column Q to create a list of unique values, so if there is data in that column in your spreadsheet, you'll want to revise the column. You will also probably want to add a path to the filenames; I didn't bother with all that, so it's currently saving to your default folder.

Finally, I didn't see what version of Excel you're using, so the SORT(UNIQUE) formulas may not work for you (though I certainly hope it does; I spent hours working this out!) ;)


VBA Code:
[FONT=Calibri]Sub SianCCodeFactory()
'Crafted 13 Mar 2023 by Wookiee At MrExcel.com
 
 
'Declare Variables
Dim lngLastRow   As Long
Dim lngLoop      As Long
Dim lngLoop2     As Long
Dim strLevel     As String
Dim strName      As String
Dim arrNames     As Variant
Dim arrSheets    As Variant
Dim rngCells     As Range
Dim wksCopy      As Worksheet
Dim wksMaster    As Worksheet
Dim wksOutput    As Worksheet
 
Set wksMaster = ActiveSheet
 
With wksMaster
 
  Application.ScreenUpdating = False
 
  'Get List Of Unique Names
  lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
 
  .Range("Q1").Formula2R1C1 = _
    "=SORT(UNIQUE(R2C3:R" & lngLastRow & "C3))"
 
  lngLastRow = .Cells(.Rows.Count, "Q").End(xlUp).Row
  Set rngCells = .Range("Q1:Q" & lngLastRow)
  arrNames = .Range("Q1:Q" & lngLastRow).Value
  rngCells.ClearContents
 
  'Copy Each Name To New Workbook
  For lngLoop = LBound(arrNames) To UBound(arrNames)
 
    strName = arrNames(lngLoop, 1)
    wksMaster.Copy
   
    Set wksOutput = ActiveSheet
 
    With wksOutput
 
      .Name = strName
 
      'Delete Non-Matching Names
      Call SortFilterDelete(wksOutput, "C", "Keep", strName)
 
      lngLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
 
      'Get List Of Unique Values
      .Range("Q1").Formula2R1C1 = _
        "=SORT(UNIQUE(R2C4:R" & lngLastRow & "C4))"
 
      lngLastRow = .Cells(.Rows.Count, "Q").End(xlUp).Row
 
      Set rngCells = .Range("Q1:Q" & lngLastRow)
      rngCells = rngCells.Value
      arrSheets = .Range("Q1:Q" & lngLastRow).Value
 
      rngCells.ClearContents
 
      lngLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
 
      'Single Records Skip Sort/Filter/Deletion
      If lngLastRow = 2 Then
 
        strLevel = .Range("D2")
        .Name = strLevel
        GoTo ThaDanceFloor
 
      Else
 
        For lngLoop2 = 2 To lngLastRow
 
          strLevel = .Range("D" & lngLoop2)
          .Copy After:=wksOutput
          Set wksCopy = ActiveSheet
 
          With wksCopy
       
            .Name = strLevel
            Call SortFilterDelete _
              (wksCopy, "D", "Keep", strLevel)
 
          End With
         
        Next lngLoop2
 
        .Visible = xlSheetHidden
 
      End If
 
ThaDanceFloor:
 
    .Parent.SaveAs Filename:=strName
 
    End With
 
  Next lngLoop
 
End With
 
Application.ScreenUpdating = True
 
 
End Sub[/FONT]

VBA Code:
[FONT=Calibri]Sub SortFilterDelete _
  (wSheet As Worksheet, sCol As String, _
   sAct As String, sCriteria As String)
'v1.55   24 Feb 2023
'Crafted 17 Feb 2016 by Jason B White
 
 
'Declare Variables
Dim lngColumn      As Long
Dim lngLastRow     As Long
Dim lngMatches     As Long
Dim strCrit        As String
Dim rngCol         As Range
 
lngColumn = Range(sCol & "1").Column
 
With wSheet
  
   .Activate
  
   'Set Variable Values
   lngLastRow = .Cells _
      (Rows.Count, lngColumn).End(xlUp).Row
  
   Set rngCol = Range _
      (.Cells(2, lngColumn), .Cells(lngLastRow, lngColumn))
  
   Select Case sAct
  
      Case Is = "Keep"
     
          strCrit = "<>" & sCriteria
         
      Case Is = "Delete"
     
          strCrit = "=" & sCriteria
          
   End Select
  
   'Determine How Many Records Match Deletion Criteria
   lngMatches = WorksheetFunction _
      .CountIf(rngCol, sCriteria)
  
   If lngMatches > 0 Then
     
      With .AutoFilter.Sort
  
         .SortFields.Clear
         .SortFields.Add _
             Key:=rngCol, _
             SortOn:=xlSortOnValues, _
             Order:=xlAscending, _
             DataOption:=xlSortTextAsNumbers
         .Header = xlYes
         .Orientation = xlTopToBottom
         .Apply
      
      End With
  
      .Cells.AutoFilter Field:=lngColumn, _
         Criteria1:=strCrit
      On Error Resume Next
        rngCol.SpecialCells(xlCellTypeVisible) _
           .EntireRow.Delete
      On Error GoTo 0
      .Cells.AutoFilter Field:=lngColumn
         
   End If
 
End With
 
Set rngCol = Nothing
 
 
End Sub[/FONT]

VBA Code:
Sub QuietlyKill _
   (sTab As Worksheet)
'v1.10   30 Aug 2018
'Deletes A Worksheet Without Displaying Alert
'Crafted 15 Mar 2018 by Jason B White


Dim booAlertsDisplayed  As Boolean
booAlertsDisplayed = Application.DisplayAlerts

Application.DisplayAlerts = False
sTab.Delete
Application.DisplayAlerts = booAlertsDisplayed


End Sub


 
Upvote 0
I'm not sure why the first two macros have font tags on them. For that matter, it just occured to me that I could include all 3 macros in one tag and save you some hassle (I just posted them that way because I copy/pasted each one individually). I hope this version is easier for you to use and gives you what you need. Cheers!

VBA Code:
Sub QuietlyKill _
   (sTab As Worksheet)
'v1.10   30 Aug 2018
'Deletes A Worksheet Without Displaying Alert
'Crafted 15 Mar 2018 by Jason B White


Dim booAlertsDisplayed  As Boolean
booAlertsDisplayed = Application.DisplayAlerts

Application.DisplayAlerts = False
sTab.Delete
Application.DisplayAlerts = booAlertsDisplayed


End Sub


Sub SortFilterDelete _
  (wSheet As Worksheet, sCol As String, _
   sAct As String, sCriteria As String)
'v1.55   24 Feb 2023
'Crafted 17 Feb 2016 by Jason B White



'Declare Variables
Dim lngColumn      As Long
Dim lngLastRow     As Long
Dim lngMatches     As Long
Dim strCrit        As String
Dim rngCol         As Range

lngColumn = Range(sCol & "1").Column

With wSheet
   
   .Activate
   
   'Set Variable Values
   lngLastRow = .Cells _
      (Rows.Count, lngColumn).End(xlUp).Row
   
   Set rngCol = Range _
      (.Cells(2, lngColumn), .Cells(lngLastRow, lngColumn))
   
   Select Case sAct
   
      Case Is = "Keep"
      
          strCrit = "<>" & sCriteria
          
      Case Is = "Delete"
      
          strCrit = "=" & sCriteria
           
   End Select
   
   'Determine How Many Records Match Deletion Criteria
   lngMatches = WorksheetFunction _
      .CountIf(rngCol, sCriteria)
   
   If lngMatches > 0 Then
      
      With .AutoFilter.Sort
   
         .SortFields.Clear
         .SortFields.Add _
             Key:=rngCol, _
             SortOn:=xlSortOnValues, _
             Order:=xlAscending, _
             DataOption:=xlSortTextAsNumbers
         .Header = xlYes
         .Orientation = xlTopToBottom
         .Apply
       
      End With
   
      .Cells.AutoFilter Field:=lngColumn, _
         Criteria1:=strCrit
      On Error Resume Next
        rngCol.SpecialCells(xlCellTypeVisible) _
           .EntireRow.Delete
      On Error GoTo 0
      .Cells.AutoFilter Field:=lngColumn
          
   End If

End With

Set rngCol = Nothing


End Sub



Sub SianCCodeFactory()
'Crafted 13 Mar 2023 by Wookiee At MrExcel.com


'Declare Variables
Dim lngLastRow   As Long
Dim lngLoop      As Long
Dim lngLoop2     As Long
Dim strLevel     As String
Dim strName      As String
Dim arrNames     As Variant
Dim arrSheets    As Variant
Dim rngCells     As Range
Dim wksCopy      As Worksheet
Dim wksMaster    As Worksheet
Dim wksOutput    As Worksheet

Set wksMaster = ActiveSheet

With wksMaster

  Application.ScreenUpdating = False
  
  'Get List Of Unique Names
  lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

  .Range("Q1").Formula2R1C1 = _
    "=SORT(UNIQUE(R2C3:R" & lngLastRow & "C3))"

  lngLastRow = .Cells(.Rows.Count, "Q").End(xlUp).Row
  Set rngCells = .Range("Q1:Q" & lngLastRow)
  arrNames = .Range("Q1:Q" & lngLastRow).Value
  rngCells.ClearContents

  'Copy Each Name To New Workbook
  For lngLoop = LBound(arrNames) To UBound(arrNames)
  
    strName = arrNames(lngLoop, 1)
    wksMaster.Copy
    
    Set wksOutput = ActiveSheet

    With wksOutput

      .Name = strName

      'Delete Non-Matching Names
      Call SortFilterDelete(wksOutput, "C", "Keep", strName)

      lngLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row

      'Get List Of Unique Values
      .Range("Q1").Formula2R1C1 = _
        "=SORT(UNIQUE(R2C4:R" & lngLastRow & "C4))"

      lngLastRow = .Cells(.Rows.Count, "Q").End(xlUp).Row

      Set rngCells = .Range("Q1:Q" & lngLastRow)
      rngCells = rngCells.Value
      arrSheets = .Range("Q1:Q" & lngLastRow).Value

      rngCells.ClearContents

      lngLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row

      'Single Records Skip Sort/Filter/Deletion
      If lngLastRow = 2 Then

        strLevel = .Range("D2")
        .Name = strLevel
        GoTo ThaDanceFloor

      Else

        For lngLoop2 = 2 To lngLastRow

          strLevel = .Range("D" & lngLoop2)
          .Copy After:=wksOutput
          Set wksCopy = ActiveSheet

          With wksCopy
        
            .Name = strLevel
            Call SortFilterDelete _
              (wksCopy, "D", "Keep", strLevel)

          End With
          
        Next lngLoop2

        .Visible = xlSheetHidden

      End If

ThaDanceFloor:

    .Parent.SaveAs Filename:=strName

    End With

  Next lngLoop
  
End With

Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,276
Messages
6,171,139
Members
452,381
Latest member
Nova88

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