VBA help

ChrisFoster

Board Regular
Joined
Jun 21, 2019
Messages
246
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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

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