Very much a VBA beginner. Trying to split data into multiple sheets based on email addresses in Column C. Essentially filtering by each email address and pasting (including header) into it's own sheet. I feel like my ranges are defined, but getting runtime error 424 when i get to the Copy After where the "warning" symbol is posted below:
Code:
Sub SPLIT_CELLS()
'
' SPLIT_CELLS Macro
'
Dim Masters, Splits As Range
Set Masters = Worksheets("Master").Cells
'1. Filter out blanks on "Email To:"
Sheets("MASTER").Activate
Range("A1").Select
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1:D" & Lastrow).AutoFilter Field:=3, Criteria1:="<>"
'2. Copy Visible cells
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("C2:C" & Lastrow).SpecialCells(xlCellTypeVisible).Copy
'3. Create a new worksheet
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Temp"
End With
'4. Paste "Email To:"
Worksheets("Temp").Activate
Range("A1").Select
Range("A1").PasteSpecial xlPasteValues
'5. Remove Duplicates
With ActiveSheet
Lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Range("A1:A" & Lastrow).Select
End With
ActiveSheet.Range("A1:A" & Lastrow).RemoveDuplicates Columns:=Array(1)
Set Splits = Worksheets("Temp").Cells
For Each cell In Splits
Sheets("Master").Copy After:=Worksheets(Sheets.Count) :warning:
ActiveSheet.Name = cell.Value
With ActiveWorkbook.Sheets(cell.Value).Range("Masters")
.AutoFilter Field:=3, Criteria1:="<>" & cell.Value, Operator:=xlFilterValues
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
ActiveSheet.AutoFilter.ShowAllData
Next cell
End Sub