Changing If Else to Case statement

iisailor

Board Regular
Joined
Feb 18, 2009
Messages
58
The advice of users on this site im trying now to change my If statements to Case statements. Ive been able to handle it with no problems until now. Im trying now to change this:
Code:
If Range("B" & StockSearch).Value = Range("A" & OrderRange).Value And _
                Range("C" & StockSearch).Value = Range("B" & OrderRange).Value And _
                Range("D" & StockSearch).Value = Range("C" & OrderRange).Value Then
The problem is i dont know how to use the And_ in a case statement.
Any help greatly appreciated
Chris
 

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.
Hi Chris

Often case select statements are used where you have a single variable that can contain many possible values, each of which is then used for a certain path of logic. So instead of having something like:

if x =1 then
...
elseif x=2 then
...
elseif x= 3 then
...
etc.
end if

Instead you have something like:

Select Case x
case 1
...
case 2
...
etc.
End Select

So in conclusion I'm not sure you want to use select case where you are testing multiple variables (per your sample). If however, what we are seeing is part of a much larger network of conditions, then it may be ok to use select case - but we would probably need to see the rest of the code. For instance, there is nothing preventing you from using select case at the highest level, and then nested if statements if you have certain logic paths you want to follow........in summary it depends, but we probably need to see more of what you are trying to do.


Andrew
 
Last edited:
Upvote 0

Forum statistics

Threads
1,203,174
Messages
6,053,908
Members
444,694
Latest member
JacquiDaly

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