I assume that there are no empty cells in the column "ModCategory" and each unique value could be used as filename.
I also assume that your headings are in the first row.
Copy the code below into a normal module.
Andreas.
Option Explicit
Sub Main()
Dim R As Range
Dim Items
Dim i As Long, c As Long
Dim WB As Workbook
'Supress messages from "SaveAs" dialog
Application.DisplayAlerts = False
'Screen off, runs faster
Application.ScreenUpdating = False
'The current sheet must contain the column
With ActiveSheet
'Find the column "ModCategory"
Set R = .Rows(1).Find("ModCategory", LookIn:=xlValues, LookAt:=xlWhole)
If R Is Nothing Then
MsgBox "Column ""ModCategory"" not found."
Exit Sub
End If
'Get the column number
c = R.Column
'Build the unique items
Items = UniqueItems(R.EntireColumn)
'Be sure the filter is off
If .FilterMode Then .ShowAllData
'For each item except the header
For i = 1 To UBound(Items)
'Set the autofilter
.Range("A1").AutoFilter c, Items(i)
'Get the visible cells
Set R = .AutoFilter.Range.SpecialCells(xlCellTypeVisible)
'Create a new file with only one sheet
Set WB = Workbooks.Add(xlWBATWorksheet)
'Copy the cells
R.Copy Cells(1, 1)
'Save and close it
WB.Close True, ThisWorkbook.Path & "\" & Items(i)
Next
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Private Function UniqueItems(ByVal R As Range, _
Optional ByVal Compare As VbCompareMethod = vbBinaryCompare) As Variant
'Return an array with all unique values in R
Dim Area As Range, Data
Dim i As Long, j As Long
Dim Dict As Object 'Scripting.Dictionary
Set R = Intersect(R.Parent.UsedRange, R)
If R Is Nothing Then
UniqueItems = Array()
Exit Function
End If
Set Dict = CreateObject("Scripting.Dictionary")
Dict.CompareMode = Compare
For Each Area In R.Areas
Data = Area
If IsArray(Data) Then
For i = 1 To UBound(Data)
For j = 1 To UBound(Data, 2)
If Not Dict.Exists(Data(i, j)) Then Dict.Add Data(i, j), 0
Next
Next
Else
If Not Dict.Exists(Data) Then Dict.Add Data, 0
End If
Next
UniqueItems = Dict.Keys
End Function
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.