Forum Discussion
Spliting rows based on criterias
Based on your description, it seems like you want to split the rows in the first sheet based on the number of values in each row of the second sheet. The code you provided is almost correct, but it is missing a few modifications. Here's an updated version of the code that should give you the desired result:
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
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
secondRow.Delete
Next secondRow
End Sub
The modifications made to the code include:
- Clearing the contents of rows 2 to the last row in the first sheet before splitting rows.
- Adjusting the loop through the rows in the first sheet to start from row 2 instead of row 1 since the first row contains the numbers 1 to 16.
- Adding the line secondRow.Delete to delete the split row from the second sheet after splitting and copying the numbers.
With these modifications, the code should correctly split the rows in the first sheet based on the number of values in each row of the second sheet and produce the desired result.
The text and the example was created with the help of AI.
Hope this will help you.
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.
- NikolinoDEJul 01, 2023Gold Contributor
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.
- presdetoisJul 02, 2023Copper ContributorI'm uncertain whether you comprehended the issue I'm attempting to resolve if I execute the code exactly as you provided it to me.
. When I ran the provided code, it resulted in a runtime error (1004) with the message "no cells were found." To investigate further, I added a second row in the first sheet, but it caused the code to delete the second row in the first sheet and all rows in the second sheet. Please refer to the example I mentioned in my initial post but I would like to illustrate a simpler example.
Let's consider this simplified example to address the issue:
You have two sheets named "First Sheet" and "Second Sheet." The "First Sheet" initially contains one row with numbers ranging from 1 to 16. The "Second Sheet" has multiple rows with varying numbers, typically ranging from two to eight. For this example, let's imagine the "Second Sheet" has two rows: the first row contains the numbers 1 and 4, while the second row also has two different numbers, 1 and 4.
The desired outcome, if the code runs correctly, would be as follows: Firstly, the code should analyze the second sheet and identify the first row with the numbers 1 and 2. It should then check if these numbers are present in the first row of the "First Sheet." In this case, the first row of the "First Sheet" contains the numbers 1, 2, 3, 4, ..., 16. Consequently, the code should split this row into two: the first row would exclude the number 1, and the second row would exclude the number 2. The original row with 16 numbers would be deleted, resulting in two rows. The first row would contain 15 numbers (1, 3, 4, 5, ..., 16), and the second row would also have 15 numbers but different (2, 3, 4, 5, ..., 16).
Next, the code would continue running and compare the second row in the "Second Sheet," which contains the numbers 1 and 4. As observed, only the first row in the "First Sheet" has these two numbers together. Hence, the result now would be the first row being split again into two rows. The first row would have 14 numbers (3, 4, 5, ..., 16), and the second row would also have 14 numbers but different (1, 3, 5, ..., 16). The third row (previously the second row) would still contain 15 numbers and would not be split since it doesn't meet the criteria of having the numbers 1 and 4 together.
I highly value the time and effort you have dedicated to assisting me in resolving this problem. I sincerely wish you peace and love.- peiyezhuJul 03, 2023Bronze ContributorThe "First Sheet" initially contains one row with numbers ranging from 1 to 16. The "Second Sheet" has multiple rows with varying numbers,
can you share a excel file with these 2 sheets?