CodexBloom - Programming Q&A Platform

Excel VBA: 'Run-time scenarios 1004' when trying to delete rows based on cell value

👀 Views: 0 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
excel vba runtime-error

I'm working with a 'Run-time behavior 1004' while trying to delete rows in an Excel sheet using VBA, specifically when the cell value matches a certain criterion. My goal is to loop through a range of cells in Column A and delete any row where the cell value equals 'DeleteMe'. Here's the relevant portion of my code: ```vba Sub DeleteRowsBasedOnValue() Dim ws As Worksheet Dim i As Long Set ws = ThisWorkbook.Sheets("Sheet1") ' Loop through rows from bottom to top For i = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row To 1 Step -1 If ws.Cells(i, 1).Value = "DeleteMe" Then ws.Rows(i).Delete End If Next i End Sub ``` The code seems straightforward, but I keep running into that behavior when it tries to delete a row. I noticed that it happens more frequently when the last row of the sheet has mixed data types (text and numbers). I've also attempted to use `Application.DisplayAlerts = False` to suppress any alerts, but the behavior still occurs. I've verified that the sheet name is correct and that the data types are consistent in Column A. What could be causing this behavior, and how can I safely delete rows without running into this scenario?