I'm writing a conditional statement in vba like
if(userID = 1 or userID = 2 or userID = 3 or userID = 4) then
...
I was wondering if there's a quicker, cleaner way to do this. Something like
if(userID in (1,2,3,4)) then
...
Thanks
An alternative would be:
select case userID
case 1,2,3,4,5,6
' do something
end select
It conveys very good the meaning of the if ... then ... else construct.
Another way
If UBound(Filter(Array(1, 2, 3, 4, 5, 6), UserID)) > -1 Then
Filter returns an array with the match. If there's no match, ubound = -1.
You can use the Application.Match function on an array:
If Not IsError(Application.Match(userID, Split("1,2,3,4",","))) Then...
CW because this matches the hypothetical example, but not likely a real use situation. However, Like is a good keyword to know.
If userID Like "[1-6]" Then
This is ok for single digit checks, but not real world multi-character user IDs.
i.e.
userID = 1
If userID Like "[1-6]" Then ' result is True
but
userID = 11
If userID Like "[1-6]" Then ' result is False
You could make a basic function like this:
Function InArray(Match, SourceArray)
InArray = False
For i = LBound(SourceArray) To UBound(SourceArray)
If SourceArray(i) = Match Then
InArray = True
Exit Function
End If
Next
End Function
Then you could say:
if InArray(userId, array(1,2,3,4)) then msgbox "Found it!"
Related
I'm using Excel 2007, I am trying to do everything with one macro. Which is a combination of multiple if's.
If E147="ESM7" And C147="Bot" Then the value shown in "O150", if not, then the value shown in "Q150".
But If E147="ESU7" Then the value shown in "O151", if not, then the value shown in "Q151".
So, there are 4 if scenarios, beginning with the main cell is "ESM7" or "ESU7" ,then depending if the next cell value is either a "Bot" or "SLD".
Try using Select Case for these scenarios, also allows more flexibility in the future:
Option Explicit
Sub Test1()
Select Case Range("E147").Value
Case "ESM7"
If Range("C147").Value = "Bot" Then
Range("O150").Value = Val
Else
Range("Q150").Value = Val
End If
Case "ESU7"
Range("O151").Value = Val
Case Else
Range("Q151").Value = Val
End Select
End Sub
val = 'value you want
If Range("E147").Value = "ESM7" And Range("C147").Value = "Bot" Then
Range("O150").Value = val
Else
If Range("E147").Value = "ESU7" Then
Range("Q151").Value =val
Else
Range("Q150").Value = val
End If
End If
You may want to try using the CHOOSE function.
https://www.techonthenet.com/excel/formulas/choose.php
Alternatively, take a look at the LOOKUP function.
http://www.excelfunctions.net/VBA-Vlookup.html
You may find that it is easier to use that, rather than multiple IF statements, if you start to get overwhelmed with too many IFs.
I want to make an if statement with conditions that have 2 result
Example
if (id = "25" OR "36") then
print "The id is 25 or 36"
else
print "the id is not 25 or 36"
end if
My concern is in the if condition statement for "OR"
I try with "AND" but this only takes id = 25 as true whereas id = 36 as false
I try with "OR" "ORELSE" "XOR" this takes everything as true.
I try || sign but I got syntax error
You are missing the comparision after the or:
If (id = "25" Or id = "36") Then
You need to provide the variable to check each time
If id = "25" or id = "36" Then
Alternatively, if this is likely to expand to include other numbers, you could use
If {"25","36"}.Contains(id) Then
Alternatively, you could use a Select Case statement:
Select Case id
Case "25", "36"
Print("The id is 25 or 36")
Case Else
Print("The id is not 25 or 36")
End Select
This works the same way as an if..else statement, but allows you to easily supply different test expressions.
I want to use parameter in my query but I can't handle with it
I have 3 big selects to raport and I just want to use parameter for some part of code which depends from choice
I have 3 different Where conditions
1st
..WHERE A.CANCELLED = 'FALSE' AND a.open_amount!=0 AND A.IDENTITY = '&client_id'..
2nd
...WHERE A.CANCELLED = 'FALSE' AND A.IDENTITY = '&client_id' ...
3rd
WHERE AND A.CANCELLED = 'FALSE' AND a.invoice_amount != a.open_amount AND A.IDENTITY = '&client_id'
I tried with decode but I guess it could be ok if there would be value in 2nd case but there isn't and I cant decode like this
WHERE decode(xxx,x1,'AND a.open_amount!= 0',x2,'',x3, 'AND a.invoice_amount != a.open_amount')
How I should solve that problem any tips?
Do you mean, if the first "where condition" OR the second OR the third is/are TRUE, you want the overall to be TRUE (select the row), and you are looking for a simplified way to write it? That is, without simply combining them with OR?
To achieve that, you don't need CASE and nested CASE statements or DECODE. You could do it like this:
WHERE A.CANCELLED = 'FALSE'
AND A.IDENTITY = '&client_id'
AND ( (xxx = x1 AND a.open_amount != 0) OR (xxx = x2) OR
(xxx = x3 AND a.invoice_amount != a.open_amount) )
This is more readable, the intent is clear, it will be easier to modify if needed, ...
You can try something like -
WHERE A.CANCELLED = 'FALSE'
AND A.IDENTITY = '&client_id'
AND a.open_amount <>
(CASE
WHEN x1 THEN 0
WHEN x2 THEN a.open_amount + 1 -- This needs to be something that is always TRUE, to nullify the condition
WHEN x3 THEN a.invoice_amount
END);
Edit: This is based on the assumption that a.open_amount is a NUMBER and uses a quick hack where we create an always TRUE condition like x <> x + 1. You should probably change this to whatever suits you better based on your data.
I need to return true or false rather than 1 & 0, using following query:
select if(u.id is null,false,true) status
from user u
limit 10
the above query returns status with value 0 or 1 rather than true and false,
Is there any way to fix this?
If you want, you can return the values as strings:
SELECT IF(u.id IS NULL, 'false', 'true') as status
FROM user u
LIMIT 10
TRUE/FALSE is equivalent to 1/0. It's just a matter of how your front end displays it.
If you need to return the strings "true" and "false" (which I don't suggest - handle that in the display) then you'll have to account for that as well:
IF(IF(u.id ISNULL,false,true) = 1, 'TRUE', 'FALSE')
MySQL has no boolean datatype, so you need to stick with 0 and 1 on the MySQL side:
select if(u.id is null, 0, 1) status_int
from user u
limit 10
If you prefer a boolean over 0/1 in PHP, you can cast it like this:
$status = (bool) $status_int;
I just had a similar issue in lua. For some reason if the table column type is "tinyint" and you call 1 or 0 it will return as true and false. If you change the column type to "int" it will call and return in lua as 1 and 0.
Old post but I hope this helps someone!
IF()
You are missing single quotes.
select if(u.id is null,'false','true') status
from user u
limit 10;
I need to pull back records based on a location ID, but I've got two fields that MAY contain the incoming criteria....
something like this
SELECT * FROM tbl
WHERE myVar = locationID
IF LocationID = 0
myVar = location2ID
this is a 'logical' example...
I think I can do
WHERE myVar = CASE WHEN locationID = 0 THEN location2ID ELSE locationID END
although I've read that CASE in a WHERE clause should be avoided...? why? or is this OK?
- either way it FAILS
WHERE CASE WHEN locationID=0 THEN location2ID=myVAr ELSE locationID=myVar END
also FAILS
thx
Sorry for the confusion lads - didn't mean to be "ambiguous" - looks like #2 will do what I want - but for the clarification requested here is the issue...
the table stores TWO locations, let's call then CURRENT_LOC and ORIGINAL_LOC... in most cases these will BOTH have data, but in some cases the 'thing' hasn't moved... so the CURRENT_LOC is '0'. MY issue is I'll be passing in a LOCATION ID, I want the records where CURRENT_LOC matches OR if the the CURRENT_LOC=0 then I ALSO want the ORIGINAL_LOC... where that matches...
does that help the discussion? I hope.
WHERE myVar = COALESCE(NULLIF(locationID, 0), location2ID)
Alternatively,
WHERE (
(locationID <> 0 AND myVar = locationID)
OR
(locationID = 0 AND myVar = location2ID)
)