A bug in the delete function - i need to click twice to delete the row left

rickyyy2006

New Member
Joined
Nov 21, 2016
Messages
8
Code:
Sub DeleteSettleHKDDebitAccount()


Dim checkrow As Long, lastrow As Long


With Worksheets


    lastrow = Worksheets("借出-港幣").Cells(Rows.Count, "A").End(xlUp).Row
 
    For checkrow = 3 To lastrow


        If Worksheets("借出-港幣").Cells(checkrow, "F") <> "" Then
        
        Worksheets("借出-港幣").Cells(checkrow, "A").EntireRow.Delete
    
    End If
    
    Next checkrow
    
 End With
    
End Sub

The 3 steps of my action: Imgur: The most awesome images on the Internet


So, my situation is when i click the second button, that is the lower button, it deleted row 4 and row 6. Yet, it didn't delete row 5. I need to click the button again, then it deleted row 5.

My question is why do i need to click twice to delete this 3 rows? My expectation is that i only need to click the lower button once, then it will delete all the rows with a letter "Y" in the column F.

I checked my code however i still don't know why. Can anyone help me?

Thanks a lot
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
When deleting you should start at the end and move to the start, so try reversing your loop.
Code:
   For checkrow = lastrow To 3 Step - 1

        If Worksheets("借出-港幣").Cells(checkrow, "F") <> "" Then
        
            Worksheets("借出-港幣").Cells(checkrow, "A").EntireRow.Delete
    
        End If

   Next checkrow
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top