Home » excel » excel – VBA For loop + if statement error

excel – VBA For loop + if statement error

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to create a loop that finds the value of my input box, select the cell that has the matching value of my input box and delete its the entire row.
For some reason It seems like my if condition is not working at all.

*Totalrow= my double variable for the total number of rows on my sheet
*tenroxcode = my string variable for the inputbox

For i = 1 To totalrows
    If tenroxcode = Range("E" & i).Value Then
        Range("E" & i).Select
        ActiveCell.EntireRow.Delete
    End If
Next

Could someone PLEASE help me?

How to&Answers:

You need to loop backwards:

For i = totalrows to 1 step -1
    If tenroxcode = Range("E" & i).Value Then Rows(i).Delete
Next

Answer:

Alternate that should be significantly faster.

dim r as variant

r = application.match(tenroxcode, columns("E"), 0)
do while not iserror(r)
    rows(r).entirerow.delete
    r = application.match(tenroxcode, columns("E"), 0)
loop