Hi,
I need to filter a column and copy paste results into a new workbook until all unique values in that column has been filtered for. I am receiving a 'subscript out of range' error on this line:
This is the full code:
(I saved this code in my personal workbook and would like to run it on the active workbook)
Code
Sub Filter()
Application.ScreenUpdating = False
Dim x As Range
Dim rng As Range
Dim rng1 As Range
Dim last As Long
Dim sht As String
Dim newBook As Excel.Workbook
Dim Workbk As Excel.Workbook
sht = "Sheet1"
Set Workbk = ThisWorkbook
last = Workbk.Sheets(sht).Cells(Rows.Count, "L").End(xlUp).Row
With Workbk.Sheets(sht)
Set rng = .Range("A1:L" & last)
End With
Workbk.Sheets(sht).Range("L1:L" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AA1"), Unique:=True
For Each x In Workbk.Sheets(sht).Range([AA2], Cells(Rows.Count, "AA").End(xlUp))
With rng
.AutoFilter
.AutoFilter Field:=12, Criteria1:=x.Value
.SpecialCells(xlCellTypeVisible).Copy
Set newBook = Workbooks.Add(xlWBATWorksheet)
newBook.Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
newBook.Activate
ActiveSheet.Paste
End With
newBook.SaveAs x.Value & ".xlsx"
newBook.Close SaveChanges:=False
Next x
Workbk.Sheets(sht).AutoFilterMode = False
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
End Sub
Display More