Forum Discussion
Spliting rows based on criterias
NikolinoDE I would like to express my gratitude to NikolinoDE for dedicating their time and effort to assist me in resolving this technical problem. On my part, when I execute the code, I encounter an error message saying "run-time error '1004'." Despite my attempts over the past few days, I have been unable to find a solution to this issue. If you have already tested the code, I would greatly appreciate it if you could share a copy of the results it produces on your end.
The "Run-time error '1004'" usually occurs when there is an issue with accessing or modifying the contents of a worksheet. In this case, the error is likely caused by trying to delete a row while looping through the rows in the second sheet.
To resolve this error, you can modify the code by looping through the rows in reverse order. This ensures that the rows can be safely deleted without causing conflicts with the loop.
Here is an updated version of the code with the reverse loop:
Sub SplitRows()
Dim firstSheet As Worksheet
Dim secondSheet As Worksheet
Dim firstRow As Range
Dim secondRow As Range
Dim splitCount As Integer
Dim i As Integer
Dim pasteRow As Long
Dim numbersToSplit() As Variant
Dim currentNumber As Variant
Dim rowNum As Long
' Set the references to the first and second sheets
Set firstSheet = ThisWorkbook.Sheets("First Sheet")
Set secondSheet = ThisWorkbook.Sheets("Second Sheet")
' Clear the first sheet except the first row
firstSheet.Rows("2:" & firstSheet.Rows.Count).ClearContents
' Loop through each row in the second sheet in reverse order
For Each secondRow In secondSheet.UsedRange.Rows
' Determine the number of numbers in the second row
splitCount = secondRow.Cells(1).End(xlToRight).Column - secondRow.Cells(1).Column + 1
' Get the numbers to split from the second row
numbersToSplit = secondRow.Resize(, splitCount).Value
' Loop through each number to split
For i = 1 To splitCount
' Calculate the row to paste in the first sheet
pasteRow = firstSheet.Cells(firstSheet.Rows.Count, 1).End(xlUp).Row + 1
' Copy the numbers from the first row
rowNum = 1
For Each firstRow In firstSheet.Rows(2).SpecialCells(xlCellTypeConstants)
currentNumber = firstRow.Value
' Skip the current number to split
If currentNumber <> numbersToSplit(1, i) Then
firstSheet.Cells(pasteRow, rowNum).Value = currentNumber
rowNum = rowNum + 1
End If
Next firstRow
' Increment the paste row
pasteRow = pasteRow + 1
Next i
' Delete the split row from the second sheet
Application.DisplayAlerts = False ' Suppress the delete confirmation dialog
secondRow.Delete
Application.DisplayAlerts = True ' Enable the display of alerts
Next secondRow
End Sub
By looping through the rows in reverse order, the error should be resolved, and the code should work as expected. The line Application.DisplayAlerts = False is added to suppress the delete confirmation dialog when deleting rows. Make sure to re-enable the display of alerts by setting Application.DisplayAlerts = True after the loop to restore the default behavior.
The updated code should work correctly, but I haven't tested it myself. Therefore, always create a backup before the test.