Case Statement in VBA - excel

Case statement below is not working when the condition met.
Dim TemplatePick As String
Select Case TemplatePick
Case OptCreate.Value = True
Call WebFormInfo
Case OptModify = True
Call ModifyTemplate
' many more case statement to come
End Select

Select case is used to test the one value in this case TemplatePick then the Case would be Case "A" which would fire when TemplatePick = "A"
So for this to work:
Select Case True
Case OptCreate.Value
Call WebFormInfo
Case OptModify
Call ModifyTemplate
' many more case statement to come
End Select
Now one caveat with Select Case, once it finds a match it ignores all others. In other words if OptCreate.Value is True then it will stop and not test whether OptModify is True.

Your case test expression (TemplatePick) is not the same as your expresion list (OptCreate.Value, OptMOdify). I have a hard time even understanding what your are trying to do. Properly structured it would look something like this:
Dim TemplatePick As String
Select Case TemplatePick
Case "Template 1"
Call WebFormInfo
Case "Template 2"
Call ModifyTemplate
...
case Else
'Do default behavior
End Select
More resources https://msdn.microsoft.com/en-us/library/cy37t14y.aspx

Related

how to use missing and boolean values in the same case statement in excel vba

In VBA (MS Excel 2016): How to combine missing and boolean values in a select case statement? The argument val could be passed as a boolean or not passed at all, or passed as something else unexpected.
public sub test(optional val as variant)
select case val
case true, isMissing(val): msgbox("foo")
case else: msgBox("bar")
end select
end sub
Above results in run-time error '13': Type mismatch on the "case true, isMissing(val):" line.
The preference is to use the same case statement allowing for multiple values to result in showing msgbox("foo"). Also would prefer not to set a default value for the optional argument on the function definition.
Each of these calls should work:
call test // expect msgBox("foo")
call test("abc") // expect msgBox("bar")
call test(true) // expect msgBox("foo")
While not very elegant, another option might be:
Public Sub test(Optional val As Variant)
Select Case IIf(IsMissing(val), True, val)
Case True: MsgBox ("foo")
Case Else: MsgBox ("bar")
End Select
End Sub
Found a work around. VBA allows for setting a default value when an optional argument is missing. So this worked:
public sub test(optional val as variant = true)
select case val
case true: msgbox("foo")
case else: msgBox("bar")
end select
end sub
Results as expected:
call test // "foo"
call test("abc") // "bar"
call test(true) // "foo"
Still would like to know if there is a way to do this on the case statement. Maybe there isn't and this is the correct approach.

CASE statement example

Can someone please help me identify the correct syntax for CASE statement with Spark SQL? I tried the following:
SELECT
CASE circle WHEN ("Panjab") THEN 2 END
FROM
siteinfo
where circle is a valid column name. However, I'm still unable to determine the correct approach.
This should work:
SELECT IF(circle='Panjab', 2, 0) FROM siteinfo
the correct case when syntax:
SELECT
CASE circle= "Panjab" THEN 2 END
FROM
siteinfo
or
SELECT
CASE circle= "Panjab" THEN 2 else 'some other value' END
FROM
siteinfo

Quick way to write multiple if statements

Is there a way to create about 100 IF statements like the one below in bulk rather than individually typing each one out with a different value in place of "string". I sometimes use excel to create code but I don't know how to do a line break in a function.
Will IF statements work on a single line, because then excel could be a possibility.
Any help will be much appreciated!
if dbread("column").ToString = "string" then
...
else
...
end if
if dbread("column").ToString = "string2" then
...
else
...
end if
best do a select case.
select dbread("column").tostring
case "string"
''do stuff
case "string2"
''do stuff
case else
''do your else code here
end select

VBA in Excel v2010 - Select Case command within another Select Case

I have a Excel v2010 Project I'm trying to complete, and I'm trying to use the "Select Case" command within another "Select Case" .... at the moment it doesn't seem to work... so my question is.. can it actually work and I'm doing it wrong or should I replace it with "if-else-end if" ??
Select Case LCase(Cells(i, "B").Value)
Case LCase("ABC")
Select Case LCase(Cells(i, "C").Value)
Case "DEF"
x = x + 1
Thanks Guys :)
Sure you can. Read this article.
Actually, your code doesn't work, because LCase returns lower case, but your Case "DEF" is in upper case, so you need to slightly modify your code:
Select Case LCase(Cells(i, "C").Value)
Case "def"

Excel - VBA : Simplify this If statement comparing cell to words

Easy question here.
I am currently using this in my program :
If LCase(inp_rng.Offset(1, 0).Value) = "street" Or LCase(inp_rng.Offset(1, 0)) = "ave." Then
score = score - 50
End If
It is obviously not clean but I can find a way to put it in one sentence only. What is the programming way of writing something like this:
If LCase(inp_rng.Offset(1,0).Value = ("street", "ave.", "road", "...", etc.) Then
'do something
End If
Thanks in advance!
You can use Select Case statement instead:
i = LCase(inp_rng.Offset(1,0).Value
Select Case i
Case "street", "ave.", "road"
'do something
Case Else
'do something
End Select
Alternatively you can populate all possible answers in an array and search the array for a match.
You may use Filter() array function
http://msdn.microsoft.com/en-us/library/office/aa164525(v=office.10).aspx

Resources