I've been using the below macro for several months with no issues until recently ..... no error returned but it keeps spinning!!
I finally figured out the problem: for the first few months, the workbooks had only about 100-200 rows, but recently due to merger of various positions, the workbooks now contain as much as 500,000 rows, and the large number of rows appears to have bogged down the macro. I played with reducing the number of rows and it seems to be fine for up to a few hundred rows, but the macro starts to slow down as the number increases. Can anyone help to make the macro more effective in handling large number of rows:
Sub CreateWorksheetsByUniqueValues()
Dim ws As Worksheet
Dim lastRow As Long
Dim cell As Range
Dim uniqueValues As Collection
Dim value As Variant
Dim newWorksheet As Worksheet
Dim newRow As Long
' Set the worksheet to work with
Set ws = ActiveSheet
' Find the last row in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Create a collection to store unique values from column A
Set uniqueValues = New Collection
' Loop through each cell in column A and add unique values to the collection
On Error Resume Next
For Each cell In ws.Range("A1:A" & lastRow)
uniqueValues.Add cell.Value, CStr(cell.Value)
Next cell
On Error GoTo 0
' Loop through each unique value and create a new worksheet
For Each value In uniqueValues
' Add a new worksheet to the workbook
Set newWorksheet = Worksheets.Add(After:=ws)
' Rename the new worksheet with the unique value
newWorksheet.Name = value
' Copy the entire header row to the new worksheet
ws.Rows(1).Copy newWorksheet.Rows(1)
' Initialize the target row for copying data
newRow = 2
' Loop through each row in column A and copy matching rows to the new worksheet
For Each cell In ws.Range("A1:A" & lastRow)
If cell.Value = value Then
' Copy the entire row to the new worksheet
ws.Rows(cell.Row).Copy newWorksheet.Rows(newRow)
newRow = newRow + 1
End If
Next cell
Next value
End Sub
I finally figured out the problem: for the first few months, the workbooks had only about 100-200 rows, but recently due to merger of various positions, the workbooks now contain as much as 500,000 rows, and the large number of rows appears to have bogged down the macro. I played with reducing the number of rows and it seems to be fine for up to a few hundred rows, but the macro starts to slow down as the number increases. Can anyone help to make the macro more effective in handling large number of rows:
Sub CreateWorksheetsByUniqueValues()
Dim ws As Worksheet
Dim lastRow As Long
Dim cell As Range
Dim uniqueValues As Collection
Dim value As Variant
Dim newWorksheet As Worksheet
Dim newRow As Long
' Set the worksheet to work with
Set ws = ActiveSheet
' Find the last row in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Create a collection to store unique values from column A
Set uniqueValues = New Collection
' Loop through each cell in column A and add unique values to the collection
On Error Resume Next
For Each cell In ws.Range("A1:A" & lastRow)
uniqueValues.Add cell.Value, CStr(cell.Value)
Next cell
On Error GoTo 0
' Loop through each unique value and create a new worksheet
For Each value In uniqueValues
' Add a new worksheet to the workbook
Set newWorksheet = Worksheets.Add(After:=ws)
' Rename the new worksheet with the unique value
newWorksheet.Name = value
' Copy the entire header row to the new worksheet
ws.Rows(1).Copy newWorksheet.Rows(1)
' Initialize the target row for copying data
newRow = 2
' Loop through each row in column A and copy matching rows to the new worksheet
For Each cell In ws.Range("A1:A" & lastRow)
If cell.Value = value Then
' Copy the entire row to the new worksheet
ws.Rows(cell.Row).Copy newWorksheet.Rows(newRow)
newRow = newRow + 1
End If
Next cell
Next value
End Sub