Code for deleting rows not working correctly

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
467
Office Version
  1. 365
Platform
  1. Windows
Hi I need a little help please
I have put some code together to delete an entire row if a cell in a list starting from B4 and is a variable length.
The code is sort of working but once it gets to a point where there are two or more blank cells in the list it is leaving some blank cells. I know I have got to get the code to step back on row but cannot get it to work.
Any help is much appreciated
Code:
Sub DeleteRows1()

If Sheets("Sheet1").Visible = xlSheetVisible Then

Sheets("Sheet1").Select

Dim myLstRng As Range
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
Set myLstRng = Sheets("Sheet1").Range("B4:B" & LastRow)

    For Each rng In myLstRng
        If rng = "" Then
         rng.EntireRow.Delete shift:=xlUp
    End If
  Next rng

Else
End If

    ActiveWorkbook.Save
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I believe the trick is to make your code work from the bottom to the top.

Try something like:
Code:
Sub DelRows()

Dim LastRow As Long
Dim cRow As Long


LastRow = Cells(Rows.Count, 2).End(xlUp).Row


For cRow = LastRow To 2 Step -1
    If Cells(cRow, 2) = "" Then
        Cells(cRow, 2).EntireRow.Delete shift:=xlUp
    End If
Next cRow


End Sub
 
Upvote 0
Hello Bagsy,

Besides what Misca has advised, you may find that using autofilter is a more efficient method:-


Code:
Sub DeleteRows1()

With Sheet1.Range("B3", Sheet1.Range("B" & Sheet1.Rows.Count).End(xlUp)) '----> Assumes that there are headings in Row3.
        .AutoFilter 1, ""
        .Offset(1).EntireRow.Delete
        .AutoFilter
End With

End Sub

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Thanks Misca
How do I modify this code so it stops at range B4, as it is deleting rows above my list that I need
 
Upvote 0
You're welcome Bagsy. I'm glad to have been able to help and thanks for the feed-back.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,223,991
Messages
6,175,820
Members
452,672
Latest member
missbanana

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