Compare a cell to a range then loop to compare another cell to the same range

Shmevan11

New Member
Joined
Apr 28, 2022
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
I'm trying to write a script that will check the contents of a cell and if a match is found it deletes the line containing the original cell.

Here's what I have so far:

VBA Code:
Sub test()

Dim x As Long
x = 2
Dim format As Worksheet: Set format = Sheets("Formatting")
Dim pending As Worksheet: Set pending = Sheets("Pending")
Dim line As Range: Set line = ActiveWorkbook.format.Range(Cells(x, 1), Cells(x, 11))
Dim rng As Range: Set rng = ActiveWorkbook.pending.Range("A2", Range("A2").End(xlDown))
Dim txt As Range: Set txt = ActiveWorkbook.format.Range(format.Cells(x, 1))

Do While Sheets("Formatting").Cells(x, 1).Value <> ""

If Intersect(rng, txt) Then

line.Select
Selection.Delete xlShiftUp


Else

x = x + 1

End If
Loop
End Sub


This gets an error 438 when trying to set the 'line' variable. I suspect it has something to do with the variable inside the variable but I am not sure how to correct it.

Any help would be greatly appreciated. Thanks in advance.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
The issue is that you have earlier set format = Sheets("Formatting")
Since this was not qualified to another workbook it would be in the active workbook, presuming that is also where the code is.
That is, format is already qualified to the active workbook & vba is objecting to you doing it again. So remove ActiveWorkbook from that line.
Further, you will also have trouble with that line if 'Formatting' is not the active sheet when the code is run because ..
Rich (BB code):
Set line = ActiveWorkbook.format.Range(Cells(x, 1), Cells(x, 11))
.. those two red cells, not being qualified to a worksheet will default to the active sheet. That would mean you would be trying to set a range on 'Formatting' using two cells on a different sheet: result -> error
You should use this instead (though note my final paragraph below)
VBA Code:
Set line = format.Range(format.Cells(x, 1), format.Cells(x, 11))

You will have similar issues in the next couple of lines as well.

One other piece of advice: Don't use words that vba already has as part of its vocabulary as variable, procedure or module names (eg Format is a function in vba so don't use that as a variable. Perhaps wsFormat instead for that worksheet variable name)
 
Last edited:
Upvote 0
Hi shmevan11,

in addition to what Peter_SSs pointed out using Intersect between ranges on two different sheets will return an error due to MS (most probably 1004).

I think using AutoFilter or WorksheetFunction.CountIf should do better than looping through a range.

Ciao,
Holger
 
Upvote 0
The issue is that you have earlier set format = Sheets("Formatting")
Since this was not qualified to another workbook it would be in the active workbook, presuming that is also where the code is.
That is, format is already qualified to the active workbook & vba is objecting to you doing it again. So remove ActiveWorkbook from that line.
Further, you will also have trouble with that line if 'Formatting' is not the active sheet when the code is run because ..
Rich (BB code):
Set line = ActiveWorkbook.format.Range(Cells(x, 1), Cells(x, 11))
.. those two red cells, not being qualified to a worksheet will default to the active sheet. That would mean you would be trying to set a range on 'Formatting' using two cells on a different sheet: result -> error
You should use this instead (though note my final paragraph below)
VBA Code:
Set line = format.Range(format.Cells(x, 1), format.Cells(x, 11))

You will have similar issues in the next couple of lines as well.

One other piece of advice: Don't use words that vba already has as part of its vocabulary as variable, procedure or module names (eg Format is a function in vba so don't use that as a variable. Perhaps wsFormat instead for that worksheet variable name)
Thanks for the advice!
 
Upvote 0
Hi shmevan11,

in addition to what Peter_SSs pointed out using Intersect between ranges on two different sheets will return an error due to MS (most probably 1004).

I think using AutoFilter or WorksheetFunction.CountIf should do better than looping through a range.

Ciao,
Holger
You were right, I received an error 1004. I ended up using CountIf instead. Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,256
Messages
6,123,906
Members
449,132
Latest member
Rosie14

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