How to Delete rows in Excel VBA using For loop

Опубликовано: 16 Октябрь 2024
на канале: Amarindaz
26,916
123

Support Amarindaz on Patreon: https://goo.gl/it9Gpk -~-
Title:How to Delete rows in Excel VBA using For loop

Blog: http://www.amarindaz.com
Recommended Book:http://www.amarindaz.com/go/excel-vba
Facebook page:   / amarindaz  
Recommended Excel VBA books
===================================================
http://www.amarindaz.com/go/excel-vba...
http://www.amarindaz.com/go/excel-201...
http://www.amarindaz.com/go/excel-201...
====================================================

In this video you'll learn how to delete rows using VBA when specific conditions exist and a logic behind it. If you delete the rows from top to bottom then excel will skip the row which presents below the current row so VBA command won't work properly in this case. So you need to go from bottom to top-down approach when you are using For loop. Watch this video to learn more .

Code:

Sub deleterows()
Dim lastrow
'Finding the lastrow in Column A

lastrow = ThisWorkbook.Sheets(1).Range("A65356").End(xlUp).Row
'Deleting the cell contain 1 in Column A
For i = lastrow To 2 Step -1
'Finding the cell which contain 1 in Column A

If ThisWorkbook.Sheets(1).Cells(i, 1) = "1" Then
ThisWorkbook.Sheets(1).Rows(i).EntireRow.Delete
End If

Next i
End Sub

💼 To Hire Me 👉 https://fwd.cx/WLGgpTiZW5HK

Checkout my app 👉 https://www.myfreshtools.com