Hi. I have a range of cells from $A2 to $E2. In B2 users there's text seperated by a comma. In the second sheet i'd like to see B2 info split into new row.
Not getting that, with the following code it seems to keep splitting b2 into new rows in second sheet.
Sub UpdateDataSheet()
Dim wsData As Worksheet
Dim wsDest As Worksheet
Dim lastRow As Long
Dim sourceRange As Range
Dim cell As Range
Dim splitValues() As String
Dim newRow As Long
' Set the source and destination worksheets
Set wsData = ThisWorkbook.Worksheets("Data")
Set wsDest = ThisWorkbook.Worksheets("Split")
' Find the last row in the source sheet
lastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
' Set the source range to columns A to E from row 2 to the last row
Set sourceRange = wsData.Range("A2:E" & lastRow)
' Clear the existing data in destination sheet
wsDest.Range("A2:E" & wsDest.Cells(Rows.Count, "A").End(xlUp).Row).ClearContents
' Loop through each cell in the source range
For Each cell In sourceRange
' Split the values in column B by comma
splitValues = Split(cell.Offset(0, 1).Value, ",")
' Get the corresponding values from columns A, C, D, and E
Dim valueA As String
Dim valueC As String
Dim valueD As String
Dim valueE As String
valueA = cell.Value
valueC = cell.Offset(0, 2).Value
valueD = cell.Offset(0, 3).Value
valueE = cell.Offset(0, 4).Value
' Determine the starting row for the new split values in the destination sheet
newRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Row + 1
' Copy the split values to new rows in the destination sheet
For i = LBound(splitValues) To UBound(splitValues)
wsDest.Cells(newRow, "A").Value = valueA
wsDest.Cells(newRow, "B").Value = Trim(splitValues(i))
wsDest.Cells(newRow, "C").Value = valueC
wsDest.Cells(newRow, "D").Value = valueD
wsDest.Cells(newRow, "E").Value = valueE
newRow = newRow + 1
Next i
Next cell
End Sub
Where am I going wrong? can someone please explain it to me?
test 1 | area 1 | 10 | grade 1 | 1 hours |
test 1 | area 2 | 10 | grade 1 | 1 hours |
Test 2 | area 3 | 10 | grade 1 | 10 hours |
Test 2 | area 1 | 10 | grade 1 | 10 hours |
Not getting that, with the following code it seems to keep splitting b2 into new rows in second sheet.
Sub UpdateDataSheet()
Dim wsData As Worksheet
Dim wsDest As Worksheet
Dim lastRow As Long
Dim sourceRange As Range
Dim cell As Range
Dim splitValues() As String
Dim newRow As Long
' Set the source and destination worksheets
Set wsData = ThisWorkbook.Worksheets("Data")
Set wsDest = ThisWorkbook.Worksheets("Split")
' Find the last row in the source sheet
lastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
' Set the source range to columns A to E from row 2 to the last row
Set sourceRange = wsData.Range("A2:E" & lastRow)
' Clear the existing data in destination sheet
wsDest.Range("A2:E" & wsDest.Cells(Rows.Count, "A").End(xlUp).Row).ClearContents
' Loop through each cell in the source range
For Each cell In sourceRange
' Split the values in column B by comma
splitValues = Split(cell.Offset(0, 1).Value, ",")
' Get the corresponding values from columns A, C, D, and E
Dim valueA As String
Dim valueC As String
Dim valueD As String
Dim valueE As String
valueA = cell.Value
valueC = cell.Offset(0, 2).Value
valueD = cell.Offset(0, 3).Value
valueE = cell.Offset(0, 4).Value
' Determine the starting row for the new split values in the destination sheet
newRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Row + 1
' Copy the split values to new rows in the destination sheet
For i = LBound(splitValues) To UBound(splitValues)
wsDest.Cells(newRow, "A").Value = valueA
wsDest.Cells(newRow, "B").Value = Trim(splitValues(i))
wsDest.Cells(newRow, "C").Value = valueC
wsDest.Cells(newRow, "D").Value = valueD
wsDest.Cells(newRow, "E").Value = valueE
newRow = newRow + 1
Next i
Next cell
End Sub
Where am I going wrong? can someone please explain it to me?