Vba run-time error 424 object required when using copy after

DSEIDEN76

New Member
Joined
Oct 8, 2018
Messages
11
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
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
How about
Code:
Sub dseiden76()
   Dim Cl As Range
   Dim Ws As Worksheet
   Dim Ky As Variant
   
   Set Ws = Sheets("Master")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws.Range("C2", Ws.Range("C" & Rows.count).End(xlUp))
         If Cl.Value <> "" Then .Item(Cl.Value) = Empty
      Next Cl
      For Each Ky In .Keys
         Ws.Range("A1").AutoFilter 3, Ky
         Sheets.Add(, Sheets(Sheets.count)).Name = Ky
         Ws.AutoFilter.Range.EntireRow.Copy Range("A1")
      Next Ky
   End With
   Ws.ShowAllData
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
one more quick question. How would I keep the same formatting in the new sheets? column widths, shading, etc.
 
Upvote 0
Try
Code:
Sub dseiden76()
   Dim Cl As Range
   Dim Ws As Worksheet
   Dim Ky As Variant
   
   Set Ws = Sheets("Master")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws.Range("C2", Ws.Range("C" & Rows.count).End(xlUp))
         If Cl.Value <> "" Then .Item(Cl.Value) = Empty
      Next Cl
      For Each Ky In .Keys
         Ws.Copy , Sheets(Sheets.count)
         ActiveSheet.Name = Ky
         Range("A1").AutoFilter 3, "<>" & Ky
         ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.Delete
         ActiveSheet.AutoFilterMode = False
      Next Ky
   End With
End Sub
 
Upvote 0
Excellent! Now I'm ready to work on emailing each sheet in a loop. Will come back if I run into trouble. Thank you again!
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top