In my worksheet 'Events' the following vba code deletes a row if the row value in column…
- User
In my worksheet 'Events' the following vba code deletes a row if the row value in column E is equals to 'Close' as shown below; For i = 2 To lastRow If LCase(eventsSheet.Range("E" & i).Value) = "close" Then response = MsgBox("Do you want to delete this row?" & vbNewLine & _ eventsSheet.Range("B" & i).Value & vbNewLine & _ eventsSheet.Range("C" & i).Value & vbNewLine & _ eventsSheet.Range("D" & i).Value & vbNewLine & _ eventsSheet.Range("E" & i).Value, vbYesNo) If response = vbYes Then eventsSheet.Rows(i).Delete lastRow = lastRow - 1 ' Update LastRow after deleting the row i = i - 1 ' Loop counter must decrease by one to check the new row that moved up eventsSheet.Calculate ' Refresh conditional formatting End If End If If eventsSheet.Range("B" & i).Value = "" Then Exit For ' Exit loop if first empty row is reached Next i
I would like to amend the formula to do the following. For each row in worksheet 'Events' that is deleted, copy the row values only in columns A, B, C, & D, to the next available row in worksheet'Events History'
- Assistant
You can amend the code by adding the logic to copy the row values to the "Events History" worksheet. Here's the updated code:
For i = 2 To lastRow If LCase(eventsSheet.Range("E" & i).Value) = "close" Then response = MsgBox("Do you want to delete this row?" & vbNewLine & _ eventsSheet.Range("B" & i).Value & vbNewLine & _ eventsSheet.Range("C" & i).Value & vbNewLine & _ eventsSheet.Range("D" & i).Value & vbNewLine & _ eventsSheet.Range("E" & i).Value, vbYesNo) If response = vbYes Then ' Copy row values to "Events History" worksheet Dim historySheet As Worksheet Set historySheet = ThisWorkbook.Worksheets("Events History") Dim nextRow As Long nextRow = historySheet.Cells(historySheet.Rows.Count, "A").End(xlUp).Row + 1 historySheet.Range("A" & nextRow).Value = eventsSheet.Range("A" & i).Value historySheet.Range("B" & nextRow).Value = eventsSheet.Range("B" & i).Value historySheet.Range("C" & nextRow).Value = eventsSheet.Range("C" & i).Value historySheet.Range("D" & nextRow).Value = eventsSheet.Range("D" & i).Value eventsSheet.Rows(i).Delete lastRow = lastRow - 1 ' Update LastRow after deleting the row i = i - 1 ' Loop counter must decrease by one to check the new row that moved up eventsSheet.Calculate ' Refresh conditional formatting End If End If If eventsSheet.Range("B" & i).Value = "" Then Exit For ' Exit loop if first empty row is reached Next i
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.