VBA help

ChrisFoster

Active Member
Joined
Jun 21, 2019
Messages
256
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have some code and I want to delete any rows where the cell in column A is "Yes".

Please can someone help?

Thanks,

Chris
 
I think you missed my reply above.
I worked it into your code below:
Code:
Sub MyDeleteRows()
    Range("A1").AutoFilter
    ActiveSheet.Range("$A$1:$S$2332").AutoFilter Field:=1, Criteria1:="Yes"
    Application.DisplayAlerts = False
    ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
    Application.DisplayAlerts = True
    Range("A1").AutoFilter
End Sub
Thanks Joe, yes I missed your first reply, apologies for that.

Your code hasnt worked either. It has kept my title line (row 1) but has deleted all data. I assume this is because 'Yes' is not present in column A. It works fine if 'Yes' is present though, which is really annoying.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
It has kept my title line (row 1) but has deleted all data. I assume this is because 'Yes' is not present in column A
Since you wanted to delete rows with "Yes" in them, I was assuming that you would have at least one row with a "Yes" in it.
However, it is a rather trivial amendment to make; simply check for the existence of at least one "Yes" first, i.e.
Code:
Sub MyDeleteRows()
    If Application.WorksheetFunction.CountIf(Range("A:A"), "Yes") > 0 Then
        Range("A1").AutoFilter
        ActiveSheet.Range("$A$1:$S$2332").AutoFilter Field:=1, Criteria1:="Yes"
        Application.DisplayAlerts = False
        ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
        Application.DisplayAlerts = True
        Range("A1").AutoFilter
    Else
        MsgBox "No rows to delete!"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,753
Messages
6,174,307
Members
452,554
Latest member
Louis1225

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