Hello,
My Aswer Is This in another thread helped me elaborating the code below (see here https://www.mrexcel.com/forum/excel...d-criteria-new-sheet-file-14.html#post4782648). It allows me to use the autofilter setting up the conditions in a sheet instead of hardcoding them. Moreover this code adds a sheet per each category, the name and the number of the sheets are part of the customizable set-up sheet.
I need to add a feature to this macro, sometimes I want the data from two categories to be copied in the same sheet instead of two different sheets. I'd like to implement it in the same macro below.
I provide an example from the dataset attached. I need German and French males to be filtered to the same sheet and Italians to another one. How is it possible to do it?
Dataset available here: https://www.dropbox.com/s/i3f9ikcr03rrfk6/new_TOY_DATASAMPLE.xlsm?dl=0
Conditions for the filter:
Screenshot from the dataset
My Aswer Is This in another thread helped me elaborating the code below (see here https://www.mrexcel.com/forum/excel...d-criteria-new-sheet-file-14.html#post4782648). It allows me to use the autofilter setting up the conditions in a sheet instead of hardcoding them. Moreover this code adds a sheet per each category, the name and the number of the sheets are part of the customizable set-up sheet.
I need to add a feature to this macro, sometimes I want the data from two categories to be copied in the same sheet instead of two different sheets. I'd like to implement it in the same macro below.
I provide an example from the dataset attached. I need German and French males to be filtered to the same sheet and Italians to another one. How is it possible to do it?
Dataset available here: https://www.dropbox.com/s/i3f9ikcr03rrfk6/new_TOY_DATASAMPLE.xlsm?dl=0
Conditions for the filter:
Screenshot from the dataset
Code:
Sub FilterMini()
'Modified 3-20-17 4:35 AM EST
'Male not Hardcoded
Dim i As Long
Dim Lastrow As Long
Dim SheetName As String
Dim Sex As String
Dim Country As String
Dim One As String
Dim Two As String
One = "Data" 'Modify name here if needed
Two = "Info" 'Modify name here if needed
Lastrow = Sheets(Two).Cells(Rows.Count, "A").End(xlUp).Row
Lastrowa = Sheets(One).Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To Lastrow
SheetName = Sheets(Two).Cells(i, "A").Value
Country = Sheets(Two).Cells(i, "B").Value
Sex = Sheets(Two).Cells(i, "C").Value
Sheets.Add(After:=Sheets(Sheets.Count)).Name = SheetName
With Sheets(One).Range("A1:G" & Lastrowa)
If Len(Country) = "0" And Sex = Sex Then
.AutoFilter Field:=7, Criteria1:="*", Operator:=xlFilterValues
.AutoFilter Field:=5, Criteria1:=Sex, Operator:=xlFilterValues
Else
If Len(Country) > 0 Then .AutoFilter Field:=7, Criteria1:=Country, Operator:=xlFilterValues
If Len(Sex) > 0 Then .AutoFilter Field:=5, Criteria1:=Sex, Operator:=xlFilterValues
End If
.SpecialCells(xlCellTypeVisible).Copy Worksheets(SheetName).Range("A1")
End With
Sheets(One).AutoFilterMode = False
Next
Sheets(One).AutoFilterMode = False
End Sub