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.
Related
I have the following code running in part of excel data query getting information from excel
`
DateTable.FiscalYear IN (?-1,?)
AND DateTable.Week = ?
AND ProductInformation.Brand = 'Drunk Elephant'
AND StoreInformation.Retailer IN CASE
WHEN ? = 'Boots' THEN ('Boots')
WHEN ? = 'Cult Beauty' THEN ('Cult Beauty')
WHEN ? = 'DTC' THEN ('DTC')
WHEN ? = 'Look Fantastic' THEN ('Look Fantastic')
WHEN ? = 'Space NK' THEN ('Space NK')
ELSE (SELECT
DISTINCT StoreInformation.Retailer
FROM
Sellout
INNER JOIN
ProductInformation
ON
Sellout.EAN = ProductInformation.EAN
INNER JOIN
StoreInformation
ON
sellout.StoreNumber = StoreInformation.StoreNumber
WHERE
ProductInformation.Brand = 'Drunk Elephant')
END
`
The ? are referencing cells in excel to bring back values. The code was running originally running fine with = CASE, but the I had to add in the final else clause, which brings back multiple results, so = no longer works. And putting the IN causes it to break, even if I remove the else and have just IN CASE.
Any ideas how to can run this case in the where clause, working if the result of the CASE is either a single value or a sub-query table?
Thanks in advance!
I have tried it with = CASE and without the ELSE, which work. Adding the ELSE brings back an error saying subquery brings back more then 1 value, which is why i tried with IN instead of =.
But having just IN without the ELSE also causes an error.
Tired brain - perhaps you can help.
My table has two bit fields:
1) TestedByPCL and
2) TestedBySPC.
Both may = 1.
The user interface has two corresponding check boxes. In the code I convert the checks to int.
int TestedBySPC = SearchSPC ? 1 : 0;
int TestedByPCL = SearchPCL ? 1 : 0;
My WHERE clause looks something like this:
WHERE TestedByPCL = {TestedByPCL.ToString()} AND TestedBySPC = {TestedBySPC.ToString()}
The problem is when only one checkbox is selected I want to return rows having the corresponding field set to 1 or both fields set to 1.
Now when both fields are set to 1 my WHERE clause requires both check boxes to be checked instead of only one.
So, if one checkbox is ticked return records with with that field = 1 , regardless of whether the other field = 1.
Second attempt (I think I've got it now):
WHERE ((TestedByPCL = {chkTestedByPCL.IsChecked} AND TestedBySPC = {chkTestedBySPC.IsChecked})
OR
(TestedByPCL = 1 AND TestedBySPC = 1 AND 1 IN ({chkTestedByPCL.IsChecked}, {chkTestedBySPC.IsChecked})))
Misunderstood the question.
Change the AND to an OR:
WHERE TestedByPCL = {chkTestedByPCL.IsChecked} OR TestedBySPC = {chkTestedBySPC.IsChecked}
Also:
SQL Server does not have a Boolean data type, it's closest option is a bit data type.
The usage of curly brackets suggests using string concatenations to build your where clause. This might not be a big deal when you're handling checkboxes but it's a security risk when handling free text input as it's an open door for SQL injection attacks. Better use parameters whenever you can.
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 have some conditions to hand on to my dataprovider. Now, my last condition I want only to take place only when the field "edit" is set to true -> In this case I want to check if the editConfirmed field 'editBevestigd' is set to true. If the 'edit' field is empty I don't want to add this last condition.
$criteria->addCondition('bevestigd = 1');
$criteria->addCondition('IF(edit = 1) editBevestigd = 1');
What is the best way to handle this. Can I do this in YII (problem here is that the record is not known yet). Or how do I write this in SQL (I know this last condition isn't right right now..)?
Thanks in advance!
If i right understand what you want then you should use this condition:
WHERE (edit = 1 AND editBevestigd = 1) OR edit = 0
Thus, the condition becomes:
$criteria->addCondition('(edit = 1 AND editBevestigd = 1) OR edit = 0');
You can try like that
//for php variable set or not
if($edit==1){
$criteria->addCondition('editBevestigd=1');
}
//for column set or not
$criteria->addCondition('edit IS NOT NULL AND editBevestigd=1');
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!"