Delete rows if cells value, across all worksheets!?

GassySpudz

New Member
Joined
Apr 30, 2019
Messages
10
Hi All,

I've gotten myself in a right mess, sulked like a child and deleted all my work!

What I would like is the main body of a macro which I will hopefully adjust to what I need and I can call upon the sub within another macro.

I want to be able to delete multiple rows if column A cells contain a certain value (with the ability to add extra criteria in the future), but across ALL sheets!
I managed to get it working for a single sheet, but whilst using this site to get it to do all sheets it kept coming up with errors and during my “fixing” I’ve broken it, threw all my toys out the pram and just deleted everything!

If anyone has a code along these lines I can use to tinker with – and hopefully not break again – that would be much appreciated.

E.G
Delete x all sheets

X 2 A delete
B 2 A Keep
X 5 B Delete
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,424
Office Version
365
Platform
Windows
Hi & welcome to MrExcel.
Will the criteria be exact values, or will it be partial matches?
 

GassySpudz

New Member
Joined
Apr 30, 2019
Messages
10
Hi!

Thank for the fast response! It will be exact values I will manually edit in the macro. I would like to do it in a data range in the future so less capable bodies could edit, though i'll figure that out myself if you know if its even possible?

Zac
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,424
Office Version
365
Platform
Windows
Ok, how about
Code:
Sub GassySpudz()
   Dim Ws As Worksheet
   Dim Ary As Variant
   
   Ary = Array("[COLOR=#ff0000]Hertfordshire[/COLOR]", "[COLOR=#ff0000]Wiltshire[/COLOR]")
   For Each Ws In Worksheets
      If Ws.AutoFilterMode Then Ws.AutoFilterMode = False
      Ws.Range("A1").AutoFilter 1, Ary, xlFilterValues
      Ws.AutoFilter.Range.Offset(1).EntireRow.Delete
      Ws.AutoFilterMode = False
   Next Ws
End Sub
Change values in red to suit.
 

GassySpudz

New Member
Joined
Apr 30, 2019
Messages
10
Hi and thanks again!

It is running and deleting the data from one work sheet but showing an error when it tries to remove the filter :( and the yellow fault line is on the .delete row on VBA

Zac
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,424
Office Version
365
Platform
Windows
What is the error message & number?
 

GassySpudz

New Member
Joined
Apr 30, 2019
Messages
10
Hi there!

Sorry for the late reply, please see error below!

Run-time error '91';

Object variable or With block variable not set

Thanks,
Zac
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,424
Office Version
365
Platform
Windows
Not sure why you would be getting that error.
Do you have any merged cells?
Also does your data always start in A1 with a header on row 1?
 

GassySpudz

New Member
Joined
Apr 30, 2019
Messages
10
Hello again!

Sorry about the late reply, I've been in and out of the office!

No merged cells, and yes A1 is the header and A2 downwards is the start of the data

Thanks,
Zac
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,424
Office Version
365
Platform
Windows
Do the value(s) that you are filtering on actually exist in the sheet?
 

Watch MrExcel Video

Forum statistics

Threads
1,101,909
Messages
5,483,660
Members
407,399
Latest member
Rakeforms

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top