Solved

Convert IF to Case

Posted on 2013-10-24
12
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 48

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 48

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 48

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
 

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 48

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 48

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 48

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

627 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