Macro to delete rows running too fast

rafael.cadina

New Member
Joined
Aug 30, 2011
Messages
4
I created a macro to delete blank rows in my sheets. When i run it in debug mode via F8 (step-by-step) it works perfect. The thig is when i run it via F5 (play) some rows are not deleted in the process. Just below is my code. Does anybody can help me out? Thanks in advance

Code:
Sub Excluir_Linhas_Em_Branco() 'Exclui todas as linhas em branco
Coluna_Base = "B"
    Linha_Final = Cells(Rows.Count, Coluna_Base).End(xlUp).Row
    Dim rng As Range
    Set rng = Range(Coluna_Base & "2:" & Coluna_Base & Linha_Final)
        For Each c In rng.Cells
            If IsEmpty(c) = True Then
            c.EntireRow.Delete
            End If
        Next c
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
To delete rows yu need to loop backwards, otherwise some may be missed.

Code:
For i = rng.Rows.Count To 1 Step -1
    If IsEmpty(rng.Cells(i, 1)) Then
        rng.Cells(i, 1).EntireRow.Delete
    End If
Next c
 
Upvote 0
You are starting at the top, moving down, and then deleting rows. Every time you do this, everything below your current location shifts up a row, so when you move to the next row you jump past something and often miss it. You will see this in break mode too, if you watch carefully

Start at the bottom, and move up instead. Use variables to count the bottom row and use "for i = endRow to 1 step -1" instead of "for each ..."
 
Upvote 0
when deleting rows (or columns) you must go backwards from bottom to top.

So you can't use a for each c in range structure.

Try

Code:
Sub Excluir_Linhas_Em_Branco() 'Exclui todas as linhas em branco
Dim i As Long, Coluna_Base As String, Linha_Final As Long

Coluna_Base = "B"
Linha_Final = Cells(Rows.Count, Coluna_Base).End(xlUp).Row

For i = Linha_Final to 2 Step -1
    If Isempty(Cells(i, Coluna_Base)) Then
        rows(i).Entirerow.Delete
    End if
Next i
End Sub
 
Upvote 0
With a loop to delete rows you have to loop backwards otherwise you will miss some rows.l However in this case there is no need to loop

Code:
Sub Excluir_Linhas_Em_Branco() 'Exclui todas as linhas em branco
Columns("B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
 
Upvote 0
does this work?
Code:
Sub DeleteBlankRow()
Dim LR As Long
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For i = LR To 2 Step -1
    If WorksheetFunction.CountA(Rows(i)) = 0 Then
        Rows(i).EntireRow.Delete
    End If
Next i
MsgBox "Done"
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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