Sub SplitListIntoWorksheets()
'split list into individual worksheets
Dim lastRow As Long, i As Long, j As Long, arrComp(0 To 2000) As Variant 'define variables
Dim shtData As Worksheet, shtDest As Worksheet
application.ScreenUpdating = False 'disable ScreenUpdating to avoid screen flashes
With Sheets("qryConsolidate")
lastRow = .range("E" & Rows.Count).End(xlUp).Row 'get last row
Set shtData = Sheets("qryConsolidate") 'ActiveSheet
j = 0
For i = 2 To lastRow
If .Cells(i, "E") <> .Cells(i - 1, "E") Then
arrComp(j) = .Cells(i, "E")
j = j + 1
End If
Next
End With
For i = 0 To j - 1
shtData.AutoFilterMode = False
shtData.range("$A$1").CurrentRegion.AutoFilter FIELD:=5, Criteria1:=arrComp(i)
Set shtDest = Sheets.Add
shtDest.Name = arrComp(i)
shtData.range("$A$1").CurrentRegion.Copy
ActiveSheet.Paste
Next
application.ScreenUpdating = True 'reenable ScreenUpdating
End Sub
Sub DeleteAllSheetsBut()
Dim sht As Worksheet
application.DisplayAlerts = False
For Each sht In ActiveWorkbook.Sheets
If sht.Name <> "Sheet1" And _
sht.Name <> "Sheet2" And _
sht.Name <> "qryConsolidate" Then _
sht.Delete
Next
application.DisplayAlerts = True
End Sub
|