Delete a Cell, Not a Row using Last Row Method

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
946
Office Version
  1. 2016
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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

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