This is my first question on the forum, but reading previous questions has been enormously helpful in the project I'm working on, so already my thanks. I couldn't find an answer to this, but apologies if I overlooked something.
I am writing an excel macro in vba, and am trying to create a select case... statement in which the expression has a variable boolean and numeric component. For example, the macro can pull "> 3" or "< 3" from another worksheet.
My hope had been that I could assign to a string all of these parameters, i.e.:
test1 = "is " & BoolOperator1 & " " & NumericValue1
and then
Select case ValuetoCompare
Case test1
'Do something
Case test2
'...
Is there a way to do this? I suppose the alternative would be to nest the case with the numeric variable inside a select function that determines the operator, but I thought this would be more elegant.
Thanks in advance for your guidance--
Josh
Assuming that you'll get a string BoolOperator1 that is a valid operator, e.g. >=, =, and a numeric value NumericValue1, the easiest way to execute this comparison on another numeric value ValueToCompare is to use the Evaluate function. This will execute a string as VBA and return it's result.
In your case, you could simply use:
If Evaluate(ValueToCompare&BoolOperator1&NumericValue1) Then ...
If you want to use this in a Select Case statement, you'd either need to use a simple If ... ElseIf ... statement - or use this trick:
Select Case True
Case Evaluate(ValueToCompare&BoolOperator1&NumericValue1): ...
Case Evaluate(ValueToCompare&BoolOperator2&NumericValue2): ...
Case Else ...
End Select
Related
Here is a code of my VBA script:
Function custom_if_formula(condition)
MsgBox(condition)
End Function
I am pasting the formula to any cell:
=custom_if_formula(B1="something")
The result in MsgBox is: TRUE or FALSE. Is it possible get in MsgBox as a result B1="something" as instead?
A pseudo-code of what I would like to achieve:
Function custom_if_formula(condition)
condition = condition.formula 'any method which take a literal string
MsgBox(condition)
End Function
P.S. My goal is to implement my own IFS function who behave identically like in Excel 2016. I am just curious if its possible. Tha'ts why I don't want to pass a string as an argument.
I think you're looking for:
Function custom_if_formula(condition)
If condition.Value = "something" Then MsgBox "something"
End Function
Where B1 is taken as the argument: =custom_if_formula(B1). Putting this in any cell (when B1 contains the string "something") will return:
You should really clarify what your intent is here, though. A UDF should return a value to its cell. Right now, it will just say 0 in the UDF's cell. Additionally, looking for "something" could be interpreted as looking for anything, and using this kind of verbiage leads to the "Who's on first, what's on second" ordeal...
Ok, I found a way how to do it:
Function custom_if_formula(condition)
cell_formula = Range(Application.Caller.Address).Formula 'I get an adress of a cell which call an UDF, and then take all string it contains
arg = Mid(cell_formula, 12, 100)
MsgBox(arg)
End Function
I'm getting a error I can't quite explain I have a Excel list I want to load into memory, to see if the next row is still a relative row I check if the cell has a value by doing If value = "" Then but the value is 1012738 and it gives me a unhandled exception...
I can't quite understand why the code is giving a error, the cell value is formatted just like all previous cells that were checked. But here a error is thrown.
Maybe i'm just not seeing it, and someone can explain?
You should check the value each time.
Dim o As Object = oSheet.Range(xxx).Value
If (o IsNot Nothing) Then
Select Case o.GetType
Case GetType(Double)
' do work here
Case GetType(Integer)
' do work here
...
End Select
Else
...
End If
Your image isn't showing up for me.
Most exceptions in excel are datatype related. Most likely, you either have a NULL or a string that looks like an integer to the human eye. You can blindly try using trim(), int() or str() etc as needed to make sure you're actually testing an integer or matching a string if thats what you are about or you can test them in a programmatic method.
First, the programmatic method of testing ... isEmpty or isNull are needed to ensure the cell is good ...
if isEmpty(value) Then
<do something>
This will most likely catch the error which is causing the message. If empty is failing, try testing with isNull. One means the cell is empty, the other means that it wasn't initialize (rarely an issue in excel, but if using vba code it can happen).
Also ... your if statement is setup in a less than optimal... used <> in place of if then + else ...
Your code ...
If value = "" Then
General formula when you want to test that something is not something else, use the not equals ...
if value <> "value" Then
"some operation"
Could somebody tell me what I'm doing wrong in this query to get a calculated column?
IIf(Left([VIN Number],2)="1F" OR "2F" OR "3F" OR "1L” OR “2L” OR “NM”,”Food”,"Comp")
It's giving me error saying "you may have entered an operand without an operator"
You can't have a compound condition like ...
Something = "a" OR "b"
You would have to repeat the Something = after OR ...
Something = "a" OR Something = "b"
For your query's IIf() expression, an In() list containing the match values would be more concise ...
IIf(Left([VIN Number],2) IN ("1F", "2F", "3F", "1L", "2L", "NM"), "Food", "Comp")
Beware your code sample includes typesetting quotes (“ and ”). Make sure you use plain quotes (") in your real code.
The OR operator doesn't work how you expect it to. It requires statements that resolve to TRUE or FALSE on either side of it, so you can't check if something is equal to one of many things like this.
You either need to do:
Iif(Left([VIN Number],2)="1F" OR Left([VIN Number],2)="2F" OR Left([VIN Number],2)="3F"...., "Food", "Comp")
or you need to do:
Iif(Left([VIN Number],2) In("1F","2F", "3F"....), "Food", "Comp")
I have a nested if statement is returning "False" rather than the expected outcome.
Scenario
Table "High VoltageCables" has data in it that default to numeric but may contain characters: kVa
Table "Master" checks "High VoltageCables" data as blank or not blank, and returns "Failed Check 1","Passed Check 1". This works fine.
Table "Meta" then checks the results of "Master" and then tests "High VoltageCables" data for length between 1 and 6, regardless of whether record is numeric or string.
Formula
=IF(MASTER!H2="Passed Check 1",IF(LEN('High VoltageCables'!O2)>=1,IF(LEN('High VoltageCables'!O2<6),"Passed Check 2","Failed Check 2")))
This is partially succesful, as it returns "Passed Check 2" for the following sample data in the source table "High VoltageCables".
1 numeric, or
1kVa str, or
50000 numeric
However if a field in "High VoltageCables"is blank, the formula returns "FALSE" rather than "Failed Check 1"
I inherited this task, (and would have preferred to do the whole thing in Access using relatively simple queries) - and unfortunately I am new to nested If statements, so I am probably missing something basic...
NB the data in High VoltageCables must default to numeric for a further check to work.
The first and second IF's seem to be missing the else part. They should be added at the end between the ))) like ), else ), else )
Every IF statement consists of IF( condition, truepart, falsepart) if you have two nested ifs it will be something like IF( condition, IF( condition2, truepart2, falsepart2), falsepart)
Hope that makes it a little clearer
You do have an unaccounted for FALSE in the middle IF. Try bring the latter two conditions together.
=IF(Master!H2="Passed Check 1",IF(OR(LEN('High VoltageCables'!O2)={1,2,3,4,5}),"Passed Check 2","Failed Check 2"))
It's still a bit unclear on what to show or not show if Master!H2 does not equal "Passed Check 1".
I failed to construct the formula with a concluding "else" - "Failed Check 1"
Using jeeped's and Tom's suggestion and adding the final "else" part I have solved the problem:
=IF(MASTER!H2="Passed Check 1",IF(OR(LEN('High VoltageCables'!O2)={1,2,3,4,5}),"Passed Check 2","Failed Check 2"),"Failed Check 1")
I need to create a string of the next type:
"<a=1 b=123 c=15 d=19 e=12345>" (rough example)
BUT if any of these variable doesn't exist, it shouldn't be printed at all. Hard to explain, but here is an example.
Desired output: <a=1 c=15 e=12345>
My current output: <a=1 b= c=15 d= e=12345>
I can do this by many case conditions, but is there a more elegant way to do this, ideally in one statement. May be something like (just what I want to find, it's not my expectation code)):
print "<[if a exists]a=" & a & ", [if b exists] b=" & b ...>"
Thanks!
Sounds like the easiest way to do this is using the iif() function. This is more or less what you ask for with [if a exists]. Not sure what condition to test because the information you have given does not make that clear. But I think you should be able to figure it out.