Delete a Cell, Not a Row using Last Row Method

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
Code:
LastRow = Range("B:D").Cells.Find("*", _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
MsgBox "The Last Non-Blank Row Is " & LastRow
For Each sht In ActiveWorkbook.Worksheets
    If sht.Visible And (sht.Name = "Growth") Then
        sht.Activate
        For r = Cells(Rows.Count, 2).End(xlUp).Row To 5 Step -1
            If Cells(r, 2).Value = 0 Then Cells(r).Delete
        Next r
    End If
Next sht

Hello All
I'm trying to delete the cell in Column B if it is a "0", instead of the entire row. The code above skips over and does not delete anything.
How do I get the cell to delete, essentially like deleting and shifting cells up with each deletion containing "0".

Thanks for the help
2013
 
Last edited by a moderator:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You are missing a few things, like your column reference in your Cells reference, and the shift up command.

So try changing:
Code:
Cells(r).Delete
to:
Code:
Cells(r, 2).Delete Shift:=xlUp
 
Upvote 0
You are missing a few things, like your column reference in your Cells reference, and the shift up command.

So try changing:
Code:
Cells(r).Delete
to:
Code:
Cells(r, 2).Delete Shift:=xlUp

That did it, thanks
 
Upvote 0
I'm trying to delete the cell in Column B if it is a "0", instead of the entire row.
Here is another way to write your code where no loops are used...
Code:
[table="width: 500"]
[tr]
	[td]Sub Test()
  On Error GoTo NoGrowthSheet
  With Sheets("Growth")
    If .Visible Then
      .Columns("B").Replace 0, "#N/A", xlWhole, , , , False, False
      .Columns("B").SpecialCells(xlConstants, xlErrors).Delete xlShiftUp
    End If
  End With
NoGrowthSheet:
End Sub
[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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