Case vba is skipping over the correct case - vba

Dim score As Variant
score = Range("KPI!A6").Value
Select Case score
Case score = "January"
Columns("D:N").EntireColumn.Hidden = True
Case score = "February"
Columns("C:C,E:N").EntireColumn.Hidden = True
Case score = "March"
Columns("C:D,F:N").EntireColumn.Hidden = True
Case score = "April"
Columns("C:E,G:N").EntireColumn.Hidden = True
Case score = "May"
Columns("C:F,H:N").EntireColumn.Hidden = True
Case score = "June"
Columns("C:G,I:N").EntireColumn.Hidden = True
Case score = "July"
Columns("C:H,J:N").EntireColumn.Hidden = True
Case score = "August"
Columns("C:I,K:N").EntireColumn.Hidden = True
Case score = "September"
Columns("C:J,L:N").EntireColumn.Hidden = True
Case score = "October"
Columns("C:K,M:N").EntireColumn.Hidden = True
Case score = "November"
Columns("C:L,N:N").EntireColumn.Hidden = True
Case score = "December"
Columns("C:M").EntireColumn.Hidden = True
End Select
End Sub
Hey all! This is my first question, so hopefully I'm not doing things incorrectly. In any case (pun intended here).... I have some relatively basic "case" code that should be looking at the cell value of A6 and hiding columns based on which month is showing. I'm having a hard time figuring out why it's skipping over the correct case (in this instance "May"). I can step through the code, but it is not stopping to perform the hide columns part. It just keeps going, checking each case and eventually hits the end of the sub and ends. I don't receive any errors, it just doesn't catch the correct value and perform the action. Any help would be appreciated. Thanks!

If score is a string such as January then score = "January" (etc) will return True (or False). True (or False) won't be equal to the value of score (i.e. "January"), so therefore none of your tests will be satisfied.
You probably meant to say:
Dim score As Variant
score = Range("KPI!A6").Value
Select Case score
Case "January"
Columns("D:N").EntireColumn.Hidden = True
Case "February"
Columns("C:C,E:N").EntireColumn.Hidden = True
Case "March"
Columns("C:D,F:N").EntireColumn.Hidden = True
Case "April"
Columns("C:E,G:N").EntireColumn.Hidden = True
Case "May"
Columns("C:F,H:N").EntireColumn.Hidden = True
Case "June"
Columns("C:G,I:N").EntireColumn.Hidden = True
Case "July"
Columns("C:H,J:N").EntireColumn.Hidden = True
Case "August"
Columns("C:I,K:N").EntireColumn.Hidden = True
Case "September"
Columns("C:J,L:N").EntireColumn.Hidden = True
Case "October"
Columns("C:K,M:N").EntireColumn.Hidden = True
Case "November"
Columns("C:L,N:N").EntireColumn.Hidden = True
Case "December"
Columns("C:M").EntireColumn.Hidden = True
End Select
End Sub
which will compare the value of score to each of the literal strings "January", "February", etc, in turn and execute the first Case where the comparison is True.
Or, as Jeeped suggests, to avoid removing score = from each of your Case statements you could use:
Dim score As Variant
score = Range("KPI!A6").Value
Select Case True
Case score = "January"
Columns("D:N").EntireColumn.Hidden = True
Case score = "February"
Columns("C:C,E:N").EntireColumn.Hidden = True
Case score = "March"
Columns("C:D,F:N").EntireColumn.Hidden = True
Case score = "April"
Columns("C:E,G:N").EntireColumn.Hidden = True
Case score = "May"
Columns("C:F,H:N").EntireColumn.Hidden = True
Case score = "June"
Columns("C:G,I:N").EntireColumn.Hidden = True
Case score = "July"
Columns("C:H,J:N").EntireColumn.Hidden = True
Case score = "August"
Columns("C:I,K:N").EntireColumn.Hidden = True
Case score = "September"
Columns("C:J,L:N").EntireColumn.Hidden = True
Case score = "October"
Columns("C:K,M:N").EntireColumn.Hidden = True
Case score = "November"
Columns("C:L,N:N").EntireColumn.Hidden = True
Case score = "December"
Columns("C:M").EntireColumn.Hidden = True
End Select
End Sub
which will compare the value True to each of the logical expressions such as score = January, score = February, etc, in turn and execute the first Case where the comparison to True returns True.
The Select Case statement is just a prettied-up If statement. The following Select Case statement
Select Case x
Case y
DoSomething
Case z
DoSomethingElse
End Select
is equivalent to the following If statement
If x = y Then
DoSomething
ElseIf x = z Then
DoSomethingElse
End If

No need to do the Select Case:
Dim score As Variant
With Worksheets("KPI")
score = .Range("A6").Value
'if score is a date remove this next line
score = DateValue(1 & " " & score & " 2017")
.Columns("C:N").Hidden = True
.Columns(Month(score) + 2).Hidden = False
End With

Related

How to simplify many "If Then Else" queries?

Can you give me a hint on how to make such code more elegant?
I need some more of these queries in the future and I would like to do it more professionally.
Thank you!
If Case = "V" Then
Case Is = "Sal"
Else
If Case = "K" Then
Case Is = "Dep"
Else
If Case = "A" Then
Case Is = "Auf"
Else
If Case = "M" Then
Case Is = "Mon"
Else
If Case = "T" Then
Case Is = "Tec"
Else
If Case = "W" Then
Case Is = "Ver"
Else
If Case = "B" Then
Case Is = "Ber"
Else
If Case = "P" Then
Case Is = "Ver"
Else
GoTo GoNext
End If
End If
End If
End If
End If
End If
End If
End If
Use Select Case
Sub test()
Dim strCode As String
Dim strVal As String
strCode = "B"
Select Case strCode
Case "A"
strVal = "Jan"
Case "B"
strVal = "Feb"
Case "C"
strVal = "Mar"
Case Else
strVal = "No match found"
End Select
End Sub
Using Select Case ... End Select
But you need to declare a variable, let us say x to be the reference
Dim x as String, y as String
'Allocate value to the `x` variable. In any way. Then:
Select Case x
Case "V": y = "Sal"
Case "K": y = "Dep"
Case "A": y = "Auf"
' and so on...
Case Else: y = "Whatever..."
End Select
Finally you obtain the y value according to the x one...
As usual, multiple ifs are simplified into one dictionary / hash table / object .
Dim variablename
Set variablename = CreateObject("Scripting.Dictionary")
variablename.Add ("V", "Sal")
variablename.Add ("K", "Dep")
....
variablename.Add ("P", "Var")
Get the variable as follows
ans = 'Nothing'
if variablename.exists("P") then
ans = variablename("P")
Rem ans = variablename.item("P") ?
Rem Equals "Var"
end if
see also https://excelmacromastery.com/vba-dictionary/
What about the following:
If Case = "V" Then
ElseIf Case = "K" Then
ElseIf Case = "A" Then
...
End If

multiple VBA if statements triggering different event

Sorry i think this is pretty basic but I was wondering if somebody could tell me why only 1 of these IF statements seem to run. The 3rd IF statement for the "CASH" option works but the other 2 unfortunately don't.
Sub HideUnhide_Discount()
If Range("Payment_Option") = "Subscription" Then
Range("MnthD_Row").EntireRow.Hidden = False
Range("MnthD").Value = 0
Else
Range("MnthD_Row").EntireRow.Hidden = True
End If
If Range("Payment_Option") = "Lease" Then
Range("OOD_Row").EntireRow.Hidden = False
Range("Leasing_Info").EntireRow.Hidden = False
Range("OOD").Value = 0
Else
Range("OOD_Row").EntireRow.Hidden = True
Range("Leasing_Info").EntireRow.Hidden = True
End If
If Range("Payment_Option") = "Cash" Then
Range("OOD_Row").EntireRow.Hidden = False
Range("MnthD_Row").EntireRow.Hidden = False
Range("OOD").Value = 0
Else
Range("OOD_Row").EntireRow.Hidden = True
Range("MnthD_Row").EntireRow.Hidden = True
End If
End Sub
Try replacing your multiple If >> Else condition with the Select Case below:
Sub HideUnhide_Discount()
' first reset all rows to be visible , later according to the value, unhide specific rows
Range("MnthD_Row").EntireRow.Hidden = True
Range("OOD_Row").EntireRow.Hidden = True
Range("Leasing_Info").EntireRow.Hidden = True
Select Case Range("Payment_Option")
Case "Subscription"
Range("MnthD_Row").EntireRow.Hidden = False
Range("MnthD").Value = 0
Case "Lease"
Range("OOD_Row").EntireRow.Hidden = False
Range("Leasing_Info").EntireRow.Hidden = False
Range("OOD").Value = 0
Case "Cash"
Range("OOD_Row").EntireRow.Hidden = False
Range("MnthD_Row").EntireRow.Hidden = False
Range("OOD").Value = 0
End Select
End Sub

Error code "Can't finde project or library" in vba

I'm making a vba project with some userforms. I want the people to click the macro button, then need to choose their initials, then by togglebutton click the weeks they want to go on holiday and then click transfer. So far i've done, that if they click one their initials, the value will be printed to a cell. The next userform (userform2), will need to look for the value, to determin wich row the values will be printed in.
But i get an error, on my Select case code.
Private Sub CommandButton1_Click()
Dim score As Integer, result As String
score = ActiveSheet.Range("A19").Value
Select Case score
Case Is = "CTK"
result = Something
Case Is = "MogM"
result = Something1
Case Is = "KSJI"
result = Something2
Case Is = "TSLR"
result = Something3
Case Is = "JOHQ"
result = Something4
Case Is = "OLGJ"
result = Something5
Case Is = "CBJN"
result = Something6
Case Is = "JVLK"
result = Something7
Case Is = "JFP"
result = Something8
Case Is = "EVAD"
result = Something9
Case Is = "TKUP"
result = Something10
Case Else
MsgBox ("Didn't find anyone with these init")
Range("A20").Value = result
End Select
End Sub
Private Sub ToggleButton1_Click()
End Sub
I solved the coding, but now i got a new question. Is it possible to simplify it?
Private Sub CommandButton1_Click()
Dim score As Integer, result As String
score = ActiveSheet.Range("A19").Value
Select Case score
Case Is = "1"
result = "CTK"
Case Is = "2"
result = "MogM"
Case Is = "3"
result = "KSJI"
Case Is = "4"
result = "TSLR"
Case Is = "5"
result = "JOHQ"
Case Is = "6"
result = "OLGJ"
Case Is = "7"
result = "CBJN"
Case Is = "8"
result = "JVLK"
Case Is = "9"
result = "JFP"
Case Is = "10"
result = "EVAD"
Case Is = "11"
result = "TKUP"
Case Else
MsgBox ("Didn't find anyone with these init")
End Select
Range("A20").Value = result
'CTK opportunities
If result = "CTK" And ToggleButton1 = True Then Range("B4").Value = "X"
If result = "CTK" And ToggleButton2 = True Then Range("C4").Value = "X"
If result = "CTK" And ToggleButton3 = True Then Range("D4").Value = "X"
If result = "CTK" And ToggleButton4 = True Then Range("E4").Value = "X"
If result = "CTK" And ToggleButton5 = True Then Range("F4").Value = "X"
If result = "CTK" And ToggleButton6 = True Then Range("G4").Value = "X"
If result = "CTK" And ToggleButton7 = True Then Range("H4").Value = "X"
If result = "CTK" And ToggleButton8 = True Then Range("I4").Value = "X"
If result = "CTK" And ToggleButton9 = True Then Range("J4").Value = "X"
If result = "CTK" And ToggleButton10 = True Then Range("K4").Value = "X"
If result = "CTK" And ToggleButton11 = True Then Range("L4").Value = "X"
If result = "CTK" And ToggleButton12 = True Then Range("M4").Value = "X"
'Next person
Unload Me
End Sub

Blackjack: Won't add Dealer's Hand

I have a problem with my blackjack game in vb.net. This code I have will add the player's score perfectly, but when it comes to the dealer's score, it will not. It only takes the second card that the dealer has.
It is called with this:
addScore("p") 'add player's score
addScore("d") 'add dealer's score
And this is "addScore()":
Public Function card(player As String, index As Integer) As Label
Try
If player = "p" Then
Return GroupBox1.Controls.OfType(Of Label).Where(Function(l) l.Name = "YouCard" & index.ToString()).Single()
ElseIf player = "d" Then
Return GroupBox1.Controls.OfType(Of Label).Where(Function(l) l.Name = "DealerCard" & index.ToString()).Single()
End If
Catch
Return Nothing
End Try
End Function
Public Sub addScore(ByVal player As String)
Dim currScore As Integer
Dim result As Integer = 0
'Add Score
For value As Integer = 1 To 7
If card(player, value).Text = "A" AndAlso (currScore + 11) <= 21 Then
result = currScore + 11
ElseIf card(player, value).Text = "A" AndAlso (currScore + 1) <= 22 Then
result = currScore + 1
ElseIf IsNumeric(card(player, value).Text) Then
result = currScore + CInt(card(player, value).Text)
ElseIf card(player, value).Text = "" Then
result = result
Else
result = currScore + 10
End If
If player = "p" Then
YouScore.Text = result
Else
DealerScore.Text = result
End If
Next
End Sub
currScore shouldn't be there. Replace it with result
Public Sub addScore(ByVal player As String)
Dim result As Integer = 0
'Add Score
For value As Integer = 1 To 7
If card(player, value).Text = "A" AndAlso (result + 11) <= 21 Then
result = result + 11
ElseIf card(player, value).Text = "A" AndAlso (result + 1) <= 22 Then
result = result + 1
ElseIf IsNumeric(card(player, value).Text) Then
result = result + CInt(card(player, value).Text)
ElseIf card(player, value).Text = "" Then
result = result
Else
result = result + 10
End If
If player = "p" Then
YouScore.Text = result
Else
DealerScore.Text = result
End If
Next
End Sub

VBA Ranking Criteria Macro - Else Without If Error

I am trying to make a macro that returns a given rank depending on the value in a cell. The value it is based on (B24) would determine the ranking and place it in B26. Below is the ranking and the code I am using. Ex. Values with over 2B should result in "1". How can I get this to work? I am currently getting "Compile error: Else without if"
Sub Criteria()
On Error GoTo catch_error
Worksheets("Sheet1").Activate
Dim score As Integer, result As String
score = Range("B24").Value
If score > 2000000000 Then result = "1"
ElseIf score >= 1500000000 And score <= 1999999999.99 Then result = "2"
ElseIf score >= 500000000 And score <= 1499999999.99 Then result = "3"
ElseIf score >= 250000000 And score <= 499999999.99 Then result = "4"
ElseIf score < 249999999.99 Then result = "Out Of Scope"
result = Range("B26").Value
Exit Sub
catch_error:
MsgBox "Some Error Occurred"
End Sub
A single line If statement can't have an Else:
If score > 2000000000 Then result = "1"
You need to restructure it like this:
If score > 2000000000 Then
result = "1"
ElseIf score >= 1500000000 And score <= 1999999999.99 Then
result = "2"
ElseIf score >= 500000000 And score <= 1499999999.99 Then
result = "3"
ElseIf score >= 250000000 And score <= 499999999.99 Then
result = "4"
ElseIf score < 249999999.99 Then
result = "Out Of Scope"
End If
Write "End if" on the line before result = Range("B26")...
Dim score As Double, not as Integer, your values are too high and they are not integers.
This should be reversed:
Range("B26").Value = result
As to what the others said:
You cannot use a single line if with Else or Else if
Score needs to be a double
The last line is reversed.
and to add
The less than arguments are not needed as once the if statment finds a true it stops looking.
Code:
Sub Criteria()
On Error GoTo catch_error
With Worksheets("Sheet1")
Dim score As Double, result As String
score = .Range("B24").Value
If score > 2000000000 Then
result = "1"
ElseIf score >= 1500000000 Then
result = "2"
ElseIf score >= 500000000 Then
result = "3"
ElseIf score >= 250000000 Then
result = "4"
Else
result = "Out Of Scope"
End If
.Range("B26").Value = result
Exit Sub
End With
catch_error:
MsgBox "Some Error Occurred"
End Sub