Any time this code runs, my top row , Row 1 values are always deleted. I want to retain…
- User
Any time this code runs, my top row , Row 1 values are always deleted. I want to retain Row 1 as my header; Sub StatusToCStatus() Dim ws1 As Worksheet, ws2 As Worksheet Dim lastRow As Integer, destRow As Integer Dim cell As Range Set ws1 = ThisWorkbook.Sheets("ClnRequests") Set ws2 = ThisWorkbook.Sheets("CStatus") ws2.Range("A2:K" & ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row).ClearContents lastRow = ws1.Cells(ws1.Rows.Count, "H").End(xlUp).Row destRow = 2 For Each cell In ws1.Range("H2:H" & lastRow) If cell.Value = "In Progress" Or cell.Value = "On Order" Or cell.Value = "To Wait" Then ws2.Range("A" & destRow & ":K" & destRow).Value = ws1.Range("A" & cell.Row & ":K" & cell.Row).Value Application.CutCopyMode = False destRow = destRow + 1 End If Next cell Call SortRowsByColumnH End Sub
- Assistant
To retain the header row (Row 1) in the destination worksheet, you can modify the code as follows:
Sub StatusToCStatus() Dim ws1 As Worksheet, ws2 As Worksheet Dim lastRow As Integer, destRow As Integer Dim cell As Range Set ws1 = ThisWorkbook.Sheets("ClnRequests") Set ws2 = ThisWorkbook.Sheets("CStatus") destRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row + 1 ' Start from the next empty row after the existing data For Each cell In ws1.Range("H2:H" & ws1.Cells(ws1.Rows.Count, "H").End(xlUp).Row) If cell.Value = "In Progress" Or cell.Value = "On Order" Or cell.Value = "To Wait" Then ws2.Range("A" & destRow & ":K" & destRow).Value = ws1.Range("A" & cell.Row & ":K" & cell.Row).Value destRow = destRow + 1 End If Next cell Call SortRowsByColumnH End SubThis modified code starts the copying process from the next empty row after the existing data in the destination worksheet (ws2), ensuring that the header row is retained.