I have a report with 4 worksheets: 1 frontpage and 3 worksheets that have to be filtered on a name. Once filtered, the sheets have to be saved as a seperate file.
I am using the following code now (see below), but I have some questions:
End Sub
Thanks in advance!
I am using the following code now (see below), but I have some questions:
- How do I remove the data that does not meet the criteria? So when the data is filtered on Name1, all other Names should be removed.
- How do I copy the frontpage (sheet1) together with the 3 filtered sheets into 1 file? It now only copies the 3 filtered sheets.
- How do I paste the data as values (it's pasted as formula now)?
VBA Code:
Option Explicit
Sub AutoFilters()
Dim sheetsToFilter As Variant, sheetName As Variant
Dim sheetsColumnToFilterOn As Variant
Dim criteria As Variant, criterium As Variant
Dim iSht As Long
Dim pre As String
sheetsToFilter = Array("Sheet2", "Sheet3", "Sheet4")
sheetsColumnToFilterOn = Array(2, 3, 4)
criteria = Array("Name1", "Name2", "Name3")
pre = Format(Now, "dd-mm-yyyy")
Application.ScreenUpdating = False
For Each criterium In criteria
For iSht = LBound(sheetsToFilter) To UBound(sheetsToFilter)
Call Autofilter(ThisWorkbook.Worksheets(sheetsToFilter(iSht)).Range("A1"), CLng(sheetsColumnToFilterOn(iSht)), CStr(criterium))
Next iSht
Call CopySheet(sheetsToFilter, ThisWorkbook.Path & "\" & criterium & " " & pre & ".xlsx")
Next criterium
Application.ScreenUpdating = True
End Sub
Sub Autofilter(rng As Range, col As Long, criteria As String)
With rng
.Autofilter
.Autofilter field:=col, Criteria1:=criteria & "*", VisibleDropDown:=True
End With
End Sub
Sub CopySheet(sheetsToFilter As Variant, shtName As String)
ThisWorkbook.Worksheets(sheetsToFilter).Copy
ActiveWorkbook.SaveAs Filename:=shtName, FileFormat:=xlWorkbookDefault
ActiveWorkbook.Close False
End Sub
Thanks in advance!