Solved

Convert IF to Case

Posted on 2013-10-24
12
333 Views
Last Modified: 2013-10-24
Folks,
I am not very good at the Case statement. The code below will not work, so I thought maybe Case would be a better option?

Private Sub cmdCkItems_Click()

If Range("C4").Text = "network" And Range("C5").Text = "7" Then
    Range("E4").Locked = False
    Range("E4").Font.Color = vbWhite
    Range("E4").Interior.Color = vbRed
    Range("E4").Value = "Correct"
Else
    Range("E4").Value = "Error"
    Range("E4").Font.Color = vbYellow
    Range("E4").Interior.Color = vbBlack
    Range("E4").Locked = True
    CheckFormulaFunction
    Exit Sub
End If

If Range("C4").Text = "components" And Range("C5").Text = "19" Then
    Range("E4").Locked = False
    Range("E4").Font.Color = vbWhite
    Range("E4").Interior.Color = vbRed
    Range("E4").Value = "Correct"
Else
    Range("E4").Value = "Error"
    Range("E4").Font.Color = vbYellow
    Range("E4").Interior.Color = vbBlack
    Range("E4").Locked = True
    CheckFormulaFunction
    Exit Sub
End If

FormulasOK

End Sub

Open in new window

0
Comment
Question by:Frank Freese
  • 6
  • 4
  • 2
12 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39598397
When you say it "will not work", what happens?
0
 
LVL 19

Assisted Solution

by:Ken Butters
Ken Butters earned 250 total points
ID: 39598430
Based on what I see, I don't think a case statement would be a better solution.  The reason I say that is it looks like your if condition is based on multiple criteria.

Looks to me like the following might be your intention but is just a guess without proper context of what you are trying to do:

Private Sub cmdCkItems_Click()

If Range("C4").text = "network" And Range("C5").text = "7" Then
    Range("E4").Locked = False
    Range("E4").Font.Color = vbWhite
    Range("E4").Interior.Color = vbRed
    Range("E4").Value = "Correct"
ElseIf Range("C4").text = "components" And Range("C5").text = "19" Then
    Range("E4").Locked = False
    Range("E4").Font.Color = vbWhite
    Range("E4").Interior.Color = vbRed
    Range("E4").Value = "Correct"
Else
    Range("E4").Value = "Error"
    Range("E4").Font.Color = vbYellow
    Range("E4").Interior.Color = vbBlack
    Range("E4").Locked = True
    CheckFormulaFunction
    Exit Sub
End If

FormulasOK

End Sub

Open in new window

or another variation that would do the same thing in this example:
Private Sub cmdCkItems_Click()

If (Range("C4").text = "network" And Range("C5").text = "7") Or (Range("C4").text = "components" And Range("C5").text = "19") Then
    Range("E4").Locked = False
    Range("E4").Font.Color = vbWhite
    Range("E4").Interior.Color = vbRed
    Range("E4").Value = "Correct"
Else
    Range("E4").Value = "Error"
    Range("E4").Font.Color = vbYellow
    Range("E4").Interior.Color = vbBlack
    Range("E4").Locked = True
    CheckFormulaFunction
    Exit Sub
End If

FormulasOK

End Sub

Open in new window

0
 

Author Comment

by:Frank Freese
ID: 39598432
The code always will stop at the first IF statement. I need to evaluate both possible conditions in cell C4
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39598454
In that case it looks like buttersk's solution will work for you but I disagree with his implication that you can't use a Case statement with multiple conditions.

Private Sub cmdCkItems_Click()

Select Case True
    Case Range("C4").text = "network" And Range("C5").text = "7"
        Range("E4").Locked = False
        Range("E4").Font.Color = vbWhite
        Range("E4").Interior.Color = vbRed
        Range("E4").Value = "Correct"
    Case Range("C4").text = "components" And Range("C5").text = "19"
        Range("E4").Locked = False
        Range("E4").Font.Color = vbWhite
        Range("E4").Interior.Color = vbRed
        Range("E4").Value = "Correct"
    Case Else
        Range("E4").Value = "Error"
        Range("E4").Font.Color = vbYellow
        Range("E4").Interior.Color = vbBlack
        Range("E4").Locked = True
End Select
    CheckFormulaFunction
    Exit Sub
End If

FormulasOK

End Sub

Open in new window

0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39598485
@martinliss - I didn't think about the fact that you could issue the select case on a boolean value of true, which in turn would allow the equivalent of any IF condition on each subsequent case check.

Nice!  I think I will find uses for that myself!
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39598509
Thanks. That's something I developed many years ago and over the years I've had discussions with a few people who believe it or not really got upset about it because in their estimation it doesn't play by the rules.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Frank Freese
ID: 39598540
Martin,
OK - great. I had to make a few changes to you code though. I moved CheckFormulaFunction and Exit Sub just before End Select and deleted the End If statement. It looks to have done the job.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39598551
I see that I left an unneeded 'End If' at line 22 when I copied and modified buttersk's code.

I'd like to see your code because moving " CheckFormulaFunction and Exit Sub just before End Select" won't work if I understand what you are saying you did.
0
 

Author Comment

by:Frank Freese
ID: 39598587
Here you go. I've go some "tweaking" on clearing:
Range("D5:F5").Value = "Error! Check you formula or category spelling"


Select Case True
    Case Range("C4").Text = "network" And Range("C5").Text = "7"
        Range("E4").Locked = False
        Range("E4").Font.Color = vbWhite
        Range("E4").Interior.Color = vbRed
        Range("E4").Value = "Correct"
    Case Range("C4").Text = "components" And Range("C5").Text = "19"
        Range("E4").Locked = False
        Range("E4").Font.Color = vbWhite
        Range("E4").Interior.Color = vbRed
        Range("E4").Value = "Correct"
    Case Else
        Range("D5:F5").Value = "Error! Check you formula or category spelling"
        Range("D5:F5").Font.Color = vbYellow
        Range("D5:F5").Interior.Color = vbBlack
        Range("D5:F5").Locked = True
        CheckFormulaFunction
        Exit Sub
End Select

Open in new window

0
 
LVL 46

Accepted Solution

by:
Martin Liss earned 250 total points
ID: 39598608
Okay, as long as you only want to do the CheckFormulaFunction procedure and Exit Sub when there's an error you should be fine.

BTW it looks like you can do this

Select Case True
    Case (Range("C4").Text = "network" And Range("C5").Text = "7") or (Range("C4").Text = "components" And Range("C5").Text = "19")
        Range("E4").Locked = False
        Range("E4").Font.Color = vbWhite
        Range("E4").Interior.Color = vbRed
        Range("E4").Value = "Correct"
    Case Else
        Range("D5:F5").Value = "Error! Check you formula or category spelling"
        Range("D5:F5").Font.Color = vbYellow
        Range("D5:F5").Interior.Color = vbBlack
        Range("D5:F5").Locked = True
        CheckFormulaFunction
        Exit Sub
End Select

Open in new window

0
 

Author Closing Comment

by:Frank Freese
ID: 39598686
thanks for all the options
I felt like I've learned a lot lately.
Good folks!
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39598700
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now