gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
This code is deleting values in Row A1. Only values below Row A1 should be deleted. How…
- User
This code is deleting values in Row A1. Only values below Row A1 should be deleted. How do I stop it : Sub StatusToCStatus() Dim ws1 As Worksheet, ws2 As Worksheet Dim lastRow As Integer, destRow As Integer Dim cell As Range
' Set the source and destination worksheets Set ws1 = ThisWorkbook.Sheets("ClnRequests") Set ws2 = ThisWorkbook.Sheets("CStatus") ' Clear the existing data in the destination sheet ws2.Range("A2:K" & ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row).ClearContents ' Find the last row in the source sheet lastRow = ws1.Cells(ws1.Rows.Count, "H").End(xlUp).Row ' Set the initial destination row destRow = 2 ' Loop through each cell in column I of the source sheet For Each cell In ws1.Range("H1:H" & lastRow) If cell.Value = "In Progress" Or cell.Value = "On Order" Or cell.Value = "To Wait" Then ' Copy the row from column A to K and paste to the destination sheet ws1.Range("A" & cell.Row & ":K" & cell.Row).Copy ws2.Range("A" & destRow).PasteSpecial Paste:=xlPasteValues Application.CutCopyMode = False ' Increase the destination row counter destRow = destRow + 1 End If Next cell Call SortRowsByColumnHEnd Sub
- Assistant
To stop the code from deleting values in Row A1, you can modify the loop to start from the second row of column H instead of the first row.
Replace this line:
For Each cell In ws1.Range("H1:H" & lastRow)With this line:
For Each cell In ws1.Range("H2:H" & lastRow)This change will ensure that only values below Row A1 are processed in the loop.