Solved

Convert IF to Case

Posted on 2013-10-24
12
324 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 45

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 45

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 45

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 45

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 45

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 45

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

760 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

18 Experts available now in Live!

Get 1:1 Help Now