Macro to Delete Rows Where there is a value in Col H from row 2 onwards

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have tried to write code to delete all rows where there is a value in Col H on sheet2 from row 2 onwards

I get a run time error "Invalid pprocedure call or argument" and the code below is highlighted


Code:
If .Cells("H" & I).Value <> "" Then

Code:
 Sub Delete_row_ColH()
Dim LR As Long, I As Long
With Sheets(2)
LR = .Cells(.Rows.Count, "A").End(xlUp).Row
For I = LR To 2 Step -1
  If .Cells("H" & I).Value <> "" Then
  .Cells("H" & I).EntireRow.Delete
End If
Next I
End With
End Sub

It would be appreciated if someone could kindly amend my code
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I don't think you can use column letters with Cells property. Try
If Range("H" & I).Value <> "" Then
Also substitute Cells with Range in the delete line: Range("H" & I).EntireRow.Delete

Shouldn't you be counting the rows in column H and not A?
 
Upvote 0
Solution
Thanks for the help Micron. Your suggestion worked 100%
 
Upvote 0
Hi Micron,

I don't think you can use column letters with Cells property.

Cells expects the rownumber first and will accept both Numbers and Letters for Columns as second argument. So correct call would be

VBA Code:
  If .Cells(I, "H").Value <> "" Then

Holger
 
Upvote 0
Without looping:
VBA Code:
Sub DeleteRows()
    On Error Resume Next
    Columns("H").Replace "*", "#N/A", xlWhole, , False
    Columns("H").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    On Error GoTo 0
End Sub
 
Upvote 0
Can you explain how that avoids row 1 as requested? I'm not seeing it. Or would it not be possible using that method?
Thanks.
 
Upvote 0
Oops! Missed that point. Thanks,
Revised code:
VBA Code:
Sub DeleteRows()
    Dim lRow As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    On Error Resume Next
    Range("H2:H" & lRow).Replace "*", "#N/A", xlWhole, , False
    Columns("H").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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