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
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
record the action

apply filter on A for yes

select the rows and delete

then it can be tidied
 
Upvote 0
record the action

apply filter on A for yes

select the rows and delete

then it can be tidied

That wont work as the data isn't always in the same order. So I'd end up deleting the incorrect rows.
Surely there is a one or two line code that allows me to do what I want?
 
Upvote 0
thats why you filter on yes, the code wont care what line it is
 
Upvote 0
It still wont work. For example, if I use the below code, it will always only ever delete row 7 as that is what the code is telling it to do.

Code:
    Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$S$2332").AutoFilter Field:=1, Criteria1:="Yes"
    Rows("7:7").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp

The other problem is what happens if 'Yes' is not present in column A? Then it would just debug at that stage anyway as it can't filter to the word 'Yes'.
 
Upvote 0
If you filter the data so that only the rows you want to see are displayed (the "Yes" rows), you can use this line of VBA code to delete them:
Code:
ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
 
Upvote 0
Code:
    Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$S$20000").AutoFilter Field:=1, Criteria1:="Yes"

    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
 
Upvote 0
Code:
    Range("A1").Select[/FONT]
[FONT=Verdana]    Selection.AutoFilter[/FONT]
[FONT=Verdana]    ActiveSheet.Range("$A$1:$S$20000").AutoFilter Field:=1, Criteria1:="Yes"[/FONT]

[FONT=Verdana]    Range(Selection, Selection.End(xlDown)).Select[/FONT]
[FONT=Verdana]    Selection.Delete Shift:=xlUp

Thanks for this. It still hasnt worked though as it just deleted the cells from column A rather than deleting the whole row. Also, if the word 'Yes' is not present in row A then it just deletes the whole of column A.
 
Last edited:
Upvote 0
I've tried to use the below code, which works fine if 'Yes' is present in column A, but it deletes all data other than row 1 if 'Yes' isnt present.

Code:
    With ActiveSheet
    .Range("A1:S1").AutoFilter 1, "Yes"
    .AutoFilter.Range.Offset(1).EntireRow.Delete
    End With

Any ideas how to skip this part if 'Yes' is not present?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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