Delete row in Book 2 depending on value of cell B4 in Book 1 - VBA

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I need to write code which loops and deletes rows in a workbook (let's call it Book 2), if the name of a row in column A in Book 2 is the same as the name of cell B4 in Book 1.

Please find below some sample data.

For Book 1, the name in cell B4 is "Coffee" in this example (but it could be "Cereals," "Cake," etc). You can copy and paste the data below or type "Coffee into cell B4 of a file called "Book 1."

[TABLE="width: 134"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Coffee[/TD]
[/TR]
</tbody>[/TABLE]

For Book 2, you can paste the data below or type "Department" in cell A2, "Coffee" in cells A3 and A4, and "Cereals" in A5 and A6. I'd then like to create code that deletes rows that contain the word "Coffee" in column A of this file (because "Coffee" is the value in cell B4 in the first file. Does anyone know how to do this? Thanks in advance.

[TABLE="width: 81"]
<colgroup><col></colgroup><tbody>[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Department[/TD]
[/TR]
[TR]
[TD]Coffee[/TD]
[/TR]
[TR]
[TD]Coffee[/TD]
[/TR]
[TR]
[TD]Cereals[/TD]
[/TR]
[TR]
[TD]Cereals[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How about
Code:
Sub Mr2017()
   With Workbooks("Book2.xlsm").Sheets("Sheet1")
      .Range("A1").AutoFilter 1, Workbooks("Book1.xlsm").Sheets("Sheet1").Range("B4")
      .AutoFilter.Range.EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Hi Fluff

Thanks for the prompt response.

When I run this, I get a 'run-time error 9' that says the subscript is out of range and it highlights this line:

.Range("A1").AutoFilter 1, Workbooks("Book1.xlsm").Sheets("Sheet1").Range("B4")

Do you know why that may be?

Thanks in advance.
 
Upvote 0
You need to change the workbook & sheet names to match what they really are.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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