VBA: Autofilter containing specific cell value from another Worksheet

Fanalis

New Member
Joined
May 28, 2024
Messages
2
Office Version
  1. 365
Hi all!

I'm trying to use the autofilter function by referencing a cell value in another Worksheet. To be more detailed:
- I am performing the autofilter on a Sheet called "Product Name"
- I have a cell value in a Sheet called "Base" (H3) that i want to look for in my Autofilter. The Autofilter should not look for the specific value, but should filter for everything CONTAINING this value.
(- the end goal is to perform this on several values (H4, H5, H6...) and if an empty cell is detected (H7 is empty for example) the autofilter does not filter for this blank value, instead the blank value is recognized and the macro is completed....but I would be super happy to receive an answer on the first part already, this "final" part would just be an icing on the cake :))

I have looked i think at every single post regarding this issue in the last two days but all the solutions provided did not work for me. I don't even remember everything I tried, but here are at least two things that did not work for me:
______________________________________________________________________________________________________________________________________________
"Solution"1
Dim rng As Range
Dim strName As String
strName = Sheet1.Cells(3, 8) 'checking this value gave me the correct value of Worksheets("Product Name") Cell H3 // Sheet1 is the worksheet called "Product Name"

'Defining range, which will be filtered
Set rng = Worksheets("Product Name").Cells
rng.AutoFilter Field:=40, Criteria1:="*" & strName & "*", Operator:=xlFilterValues
______________________________________________________________________________________________________________________________________________
"Solution"2
another solution i found was with RANGE

Dim rng As Range

'Defining range, which will be filtered
Set rng = Worksheets("Product Name").Cells
rng.AutoFilter Field:=40, Criteria1:=Sheet1."*" & Range("H3").Value & "*", Operator:=xlFilterValues
_______________________________________________________________________________________________________________________________________________

I thought this should be easy as i saw everywhere people using *, but it does not seem to work me ...

Your help would be HUGELY appreciated!!

ah and my error message is always: Runtime Error 1004: Select Method of Range class failed
after googling the error message i tried activating the appropriate sheet, but it did not help...
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
i just saw that i included "Sheet1." in the first solution but not in the second. I tried all combinations.

rng.AutoFilter Field:=40, Criteria1:=Sheet1."*" & Range("H3").Value & "*", Operator:=xlFilterValues
as well as
Sheet1.Activate
rng.AutoFilter Field:=40, Criteria1:="*" & Range("H3").Value & "*", Operator:=xlFilterValues

Same for the range-solution
 
Upvote 0

Forum statistics

Threads
1,224,867
Messages
6,181,481
Members
453,046
Latest member
Excelvbaexpert

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