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

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"

Related

Multiple nested if(ISNUMBER(FIND... is required

Is it possible to do so?
Sub test()
r = ActiveSheet.Range("v" & Rows.Count).End(xlUp).Row
Range("$W2:$W" & r & " ").Formula=
IF(ISNUMBER(FIND("7212",K2)),"laptop",
IF(ISNUMBER(FIND("774",K2)),"laptop",
IF(ISNUMBER(FIND("7745",K2)),"laptop",
IF(ISNUMBER(FIND("234",K2)),"desktop","NO")))
End Sub
I have to apply conditions like this on if condition but I am unable to achieve this.
Kindly anyone help on this
You need to enclose your formula in double quotes, but in order to do so, you need to double the double quotes within your formula, something like this:
Range("$W2:$W" & r & " ").Formula = "IF(ISNUMBER(FIND(""7212"",K2)),""laptop"", IF(ISNUMBER(FIND(""774"",K2)),""laptop"", IF(ISNUMBER(FIND(""7745"",K2)),""laptop"", IF(ISNUMBER(FIND(""234"",K2)),""desktop"",""NO"")))"
(Sorry for the bad formatting, but VBA does not allow multiline strings)
By the way, I've just edited your formula in order for it to work, but I don't understand your logic: your logic says:
If <condition1>
then "laptop"
else if <condition2>
then "laptop"
else if <condition3>
then "laptop"
else "desktop"
end if
end if
end if
Generally, for such a situation, the following logic is used:
if <condition1> OR <condition2> OR <condition3>
then "laptop"
else "desktop"
end if
This reduces the number of if-loops and makes it more readable.

Excel TEXT(dd.mm.yyyy) function and different client languages

How can I make a function with a formatted date that will work in every language? I want to make the following function:
=CONCATENATE(TEXT(C8;"TT.MM.JJJJ");"/";G8)
The problem here is, that I use the english client but because I'm a german, excel forces me to use T for day and J for year. I think this will cause problem on a PC located in england (for example).
I think [$-409] won't work because I still have to use T for day and J for year. Is there a good solution for this (function wise)?
If you pass the value of a formula in "" then it cannot be changed based on the localisation settings.
A good way to do it is to use a custom function with VBA, returning "TT.MM.JJJJ" if you are in Germany and "DD.MM.YYYY" if you are in England.
Public Function CorrectString() As String
Select Case Application.International(XlApplicationInternational.xlCountryCode)
Case 1
CorrectString = "DD.MM.YYYY"
Case 49
CorrectString = "TT.MM.JJJJ"
Case Else
CorrectString = "ERROR"
End Select
End Function
Would allow you to call the function like this:
=CONCATENATE(TEXT(C8;CorrectString());"/";G8)
And depending on the excel language, it would give either the German or the English versions.
To simplify the formula, try calling only:
=TEXT(21322;CorrectString())
This should return 17.05.1958.
Source for the regional languages, mentioned by #Dan at the comments:
https://bettersolutions.com/vba/macros/region-language.htm
Or run this to see the corresponding number of your current Excel:
MsgBox xlApplicationInternational.xlCountryCode
Just dropping in another imho elegant (VBA free) alternative taken from: Stackoverflow answer by #Taosique
=IF(TEXT(1,"mmmm")="January",[some logic for English system],[some logic for non-English system])
I've had the same problem and solved it with similar VBA Function. My function accepts input in international format, and outputs the local version for user.
Please see code below:
Function DateFormater(sFI As String)
Dim aFI() As String
aFI = split(StrConv(sFI, vbUnicode), Chr$(0))
ReDim Preserve aFI(UBound(aFI) - 1)
For i = 0 To UBound(aFI)
Select Case (aFI(i))
Case "m", "M"
DateFormater = DateFormater & Application.International(xlMonthCode)
Case "y", "Y"
DateFormater = DateFormater & Application.International(xlYearCode)
Case "d", "D"
DateFormater = DateFormater & Application.International(xlDayCode)
Case Else
DateFormater = DateFormater & aFI(i)
End Select
Next i
End Function
A simpler solution is to use generic Excel functions.
=CONCATENATE(TEXT(DAY(C8;"00");".";TEXT(MONTH(C8);"00");".";YEAR(C8);"/";G8)

Case Statement in VBA

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

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

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