Search occurrences in a range, and if not found skip their procedure

APAL1

New Member
Joined
Jun 22, 2014
Messages
13
Hello all,

My code starts with a range that the code filters and there supposed to be left 3 values after filtering.
Each value has its own sub-procedure. The range is column "B".
I would like the first sub, the one that filters, to check if what is left after filtering is actually there.
If not, the sub will not call the routine that takes care of it, and will go to the next one.
At the same time, it should check if the 2nd value is there after filtering and if not - it will not call the routine that takes care of it and the same with the 3rd one..
My values are: 205,219,218. and the may appear more than once.
so occurrences may be: 205,219,218; 205,218; 205,219; 219,218.
Would someone have an idea how to deal with this?
Thanks a lot, this site is really helpful!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this

Code:
Sub CheckValues()

    Dim rng As Range: Set rng = Range("B:B").SpecialCells(xlCellTypeVisible)
    
    If Not rng.Find(what:=205) Is Nothing Then Call Procedure205
    If Not rng.Find(what:=218) Is Nothing Then Call Procedure218
    If Not rng.Find(what:=219) Is Nothing Then Call Procedure219
    
End Sub

where Procedure205 etc is the name of a sub
 
Last edited:
Upvote 0
If there are multiple values in one cell you may need to use:

Code:
Sub CheckValues2()

    Dim rng As Range: Set rng = Range("B:B").SpecialCells(xlCellTypeVisible)
    
    If Not rng.Find(what:=205, lookat:=xlPart) Is Nothing Then Call Procedure205
    If Not rng.Find(what:=218, lookat:=xlPart) Is Nothing Then Call Procedure218
    If Not rng.Find(what:=219, lookat:=xlPart) Is Nothing Then Call Procedure219
    
End Sub
 
Upvote 0
and this looks to match the value of the cell as a whole

Code:
Sub CheckValues4()

    Dim rng As Range: Set rng = Range("B:B").SpecialCells(xlCellTypeVisible)
    
    If Not rng.Find(what:=205, lookat:=xlWhole) Is Nothing Then Call Procedure205
    If Not rng.Find(what:=218, lookat:=xlWhole) Is Nothing Then Call Procedure218
    If Not rng.Find(what:=219, lookat:=xlWhole) Is Nothing Then Call Procedure219
    
End Sub

Correct option to use depends on what the individual cells contain
Need to choose one that does not give false positives (eg 205 would be found within 2051 etc)
If you are struggling, please provide the values of a representative sample of "typical" filtered cells
- there are various methods avalable including MATCH, COUNTIF etc
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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