How to make an OR() statement with n conditions, based on the count of an input array?

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
528
Office Version
  1. 365
Platform
  1. Windows
Hello,

Suppose I want to test if a particular cell, say B2, is equal to any one of a few numbers or not, such as below:
=IF(OR(B2=10,B2=11,B2=12),operation_if_true,operation_if_false)

However, the number of the conditions may not always be 3, so I need to create an expression that automatically generates an OR() statement with the number of conditions matching the count of an input array; perhaps something like below:
=LET(x,{10,11,12,13,etc.},y,formula_to_generate_OR_with_n_conditions,IF(y,operation_if_true,operation_if_false))

I got stuck trying to code for "formula_to_generate_OR_with_n_conditions". I was thinking possibly MAP or MAKEARRAY?

Thanks for any input! 🤗
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
If you wanted to use Vba you could use Case
This script looks at numerous values you may have in Range("B2")
VBA Code:
Sub Using_Select_Case()
'Modified 11/16/2022  8:06:29 PM  EST
Application.ScreenUpdating = False
Select Case Range("B2").Value

    Case 1, 3, 6, 7
        MsgBox "Yes"

        Case 10 To 500
             MsgBox "Hello"
            
             Case "Alpha", "Bravo", "Charlie"
                MsgBox "You are great"

Case Else
    MsgBox "No"
End Select
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you! I'm a newb at VBA 🤦‍♂️😅 I was able to follow how it works. But how can I use this in the context of my example? I opened the VBA editor and pasted your code. What do I do next? 🤦‍♂️😅
 
Upvote 0
Thank you! I'm a newb at VBA 🤦‍♂️😅 I was able to follow how it works. But how can I use this in the context of my example? I opened the VBA editor and pasted your code. What do I do next? 🤦‍♂️😅
So, there are numerous ways to activate a script.
Most people create a shape and then Right click the shape and select Assign Macro
Choose the macro I gave you.
Then any time you click the button the script runs.
So try this and if you prefer to activate the macro some other way let me know.
 
Upvote 0
That worked!

Now, is it possible to somehow transfer the output of this code into an Excel formula, so that a particular operation can be done on the worksheet, based on the output?
 
Upvote 0
That worked!

Now, is it possible to somehow transfer the output of this code into an Excel formula, so that a particular operation can be done on the worksheet, based on the output?
I can have a script run when you enter a certain value in a certain cell.
Like if you enter "Alpha" in Range("A1") then the script I just gave you can run.
But this is if you enter into the cell Manually. Not if some formula causes "Alpha" to be entered into Range("A1"
If you want something like that give me the specifics
 
Upvote 0
@StephenCrump
Oh 😯, not sure why, but I wasn't aware of that syntax for OR 🤦‍♂️🤦‍♂️. (So in all my codes, I would always repeat the cell OR(B2=10,B2=11,etc.))
That's exactly what I was looking for 😅 Thank you!

@My Aswer Is This
Now that I know this OR() syntax, I think I should be able to write the rest of my code. But I will message if I get stuck. Thank you!
 
Upvote 0
@StephenCrump
Oh 😯, not sure why, but I wasn't aware of that syntax for OR 🤦‍♂️🤦‍♂️. (So in all my codes, I would always repeat the cell OR(B2=10,B2=11,etc.))
That's exactly what I was looking for 😅 Thank you!

@My Aswer Is This
Now that I know this OR() syntax, I think I should be able to write the rest of my code. But I will message if I get stuck. Thank you!
I know formulas can do a lot of things. I see some formulas that are nearly 100 characters long and I'm always surprised someone knows how to write such formulas.
But I only write Vba code. So glad to see you have another way to do what you want.
 
Upvote 0
Yes, I'm always learning something new from this forum, on a daily basis. Since Stephen is on this thread, I should say he has been super helpful along the way.

But I wish I knew more VBA like you. It's so powerful! So I'm kind of in the other end of the spectrum compared to you. I can write some complex formulas, but practically not much VBA 😅 But glad to have VBA experts on here!
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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