I am looking to take all unique values in Column D and, for each unique value, create a new sheet with that name and paste all rows with that value from Sheet1 into that new sheet. I have found a great solution to this problem that will work for small data sets but it will crash Excel with a larger number of rows (10000+). The answer was provided here by Luna Zhang: https://social.msdn.microsoft.com/Forums/office/en-US/ea461892-d5e0-4c5e-abca-6904d6a1f886/splitting-data-into-multiple-tabs-in-excel-2010
Sub CopyToTabs()
Dim ws As Worksheet
Dim wsNEW As Worksheet
Dim i As Integer
Dim j As Integer
Set ws = ActiveWorkbook.Worksheets("Sheet1")
For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
CopyConditional ws, ws.Range("D" & i).Value
Next i
End Sub
Sub CopyConditional(wsNODE As Worksheet, WhichName As String) '
Const NameCol = "D" 'takes from column D
Const FirstRow = 2 'starts at row 2
Dim LastRow As Long
Dim SrcRow As Long
Dim TrgRow As Long
Dim wsNEW As Worksheet
On Error Resume Next
Set wsNEW = Worksheets(WhichName) 'put this variable in the sheet with the same name as the variable
If wsNEW Is Nothing Then 'if there isn't already a sheet with that unique variable name create one
Set wsNEW = Worksheets.Add(After:=wsNODE) 'create new sheet after original worksheet
wsNEW.Name = WhichName 'name of the unique variable in column D
End If
On Error GoTo 0
wsNEW.Rows.Clear 'clear data in the newly created sheet
wsNODE.Rows(1).Copy Destination:=wsNEW.Cells(1, 1) 'copy and paste the data from Sheet1 and
'paste in sheet with the same name
TrgRow = wsNEW.Cells(wsNEW.Rows.Count, NameCol).End(xlUp).Row + 1 'defines Target row to add to the
'next empty row in the new sheet
LastRow = wsNODE.Cells(wsNODE.Rows.Count, NameCol).End(xlUp).Row 'defines last row as the last row
'on the NodeCal1 sheet
For SrcRow = FirstRow To LastRow 'from row 2 to the last row in NodeCal1
If wsNODE.Cells(SrcRow, NameCol) = WhichName Then 'if cell in column D = searching variable then
wsNODE.Cells(SrcRow, 1).EntireRow.Copy Destination:=wsNEW.Cells(TrgRow, 1) 'copy
TrgRow = TrgRow + 1 'paste in last empty row of the new sheet
End If
Next SrcRow
End Sub
Is there any way to reduce the time? I also tried a variant of the answer provided by Profex here: Efficient way to delete entire row if cell doesn't contain '@' and changed
Sheet.Range(DeleteAddress).EntireRow.Delete
to
Sheet.Range(CopyAddress).EntireRow.Copy
Worksheets.Add After:=Sheets(Sheets.Count)
Range("A1").Select
ActiveSheet.Paste
But it returns error 1004.