2

I keep on getting a 'mismatched type error', or some other menial error like 'no block if'. However when I fix one, the other pops back up. Not sure where the problem is in this code. It's pretty straight forward, get cell values and then classify based on the values.

Private Sub CommandButton2_Click()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Set sh1 = ActiveWorkbook.Sheets("Completed Questionnaire")
Set sh2 = ActiveWorkbook.Sheets("Classifier")

If sh1.Range("C10") = "Yes" Or sh1.Range("C11") = "Yes" Then
    If sh1.Range("C19:C20") = "Yes" Or sh1.Range("C17") = "Yes" Then
        sh2.Unprotect Password:="xxx"
        sh2.Range("D4") = "PS1"
        sh2.Protect Password:="xxx"
    End If

ElseIf sh1.Range("C10") = "Yes" Or sh1.Range("C11") = "Yes" And _
    sh1.Range("C14") = "Yes" Or _
    sh1.Range("C15") = "Yes" Or _
    sh1.Range("C16") = "Yes" Or _
    sh1.Range("C18") = "Yes" Then
        sh2.Unprotect Password:="xxx"
        sh2.Range("D4") = "PS2"
        sh2.Protect Password:="xxx"

ElseIf sh1.Range("C12") = "Yes" And sh1.Range("C21") = "Yes" Then
    If sh1.Range("C10:C11") = "No" Then
        sh2.Unprotect Password:="xxx"
        sh2.Range("D4") = "PS3"
        sh2.Protect Password:="xxx"
    End If


ElseIf sh1.Range("C13") = "Vendor Only" And sh1.Range("C10:C11") = "Yes" 
Then _ 
    If ("C14") = "Yes" Or _
    sh1.Range("C15") = "Yes" Or _
    sh1.Range("C16") = "Yes" Or _
    sh1.Range("C17") = "Yes" Or _
    sh1.Range("C18") = "Yes" Or _
    sh1.Range("C19") = "Yes" Or _
    sh1.Range("C20") = "Yes" Then _
        sh2.Unprotect Password:="xxx"
        sh2.Range("D4") = "Payment Process"
        sh2.Protect Password:="xxx"
    End If

ElseIf sh1.Range("C10:C21") = "No" Then
    sh2.Unprotect Password:="xxx"
    sh2.Range("D4") = "Non-Payment"
    sh2.Protect Password:="xxx"

Else: sh2.Unprotect Password:="xxx"
    sh2.Range("D4") = "Needs Review"
    sh2.Protect Password:="xxx"

End If
Sheets("Classifier").Select

End Sub
2
  • 1
    look for cells with errors Commented Sep 25, 2017 at 19:33
  • 1
    Also you have a Then on a separate line here - is what SO has exactly as you have it in VBA? Also, try not to use the Else: ... and just put that part on a separate line too. Commented Sep 25, 2017 at 19:33

2 Answers 2

4

Try this:

Private Sub CommandButton2_Click()
Dim sh1     As Worksheet
Dim sh2     As Worksheet
Set sh1 = ActiveWorkbook.Sheets("Completed Questionnaire")
Set sh2 = ActiveWorkbook.Sheets("Classifier")

If sh1.Range("C10") = "Yes" Or sh1.Range("C11") = "Yes" Then
    If sh1.Range("C19:C20") = "Yes" Or sh1.Range("C17") = "Yes" Then
        sh2.Unprotect Password:="xxx"
        sh2.Range("D4") = "PS1"
        sh2.Protect Password:="xxx"
    End If

ElseIf sh1.Range("C10") = "Yes" Or sh1.Range("C11") = "Yes" And _
       sh1.Range("C14") = "Yes" Or _
       sh1.Range("C15") = "Yes" Or _
       sh1.Range("C16") = "Yes" Or _
       sh1.Range("C18") = "Yes" Then
        sh2.Unprotect Password:="xxx"
        sh2.Range("D4") = "PS2"
        sh2.Protect Password:="xxx"

ElseIf sh1.Range("C12") = "Yes" And sh1.Range("C21") = "Yes" Then
    If sh1.Range("C10:C11") = "No" Then
        sh2.Unprotect Password:="xxx"
        sh2.Range("D4") = "PS3"
        sh2.Protect Password:="xxx"
    End If

ElseIf sh1.Range("C13") = "Vendor Only" And sh1.Range("C10:C11") = "Yes" Then
    If ("C14") = "Yes" Or _
       sh1.Range("C15") = "Yes" Or _
       sh1.Range("C16") = "Yes" Or _
       sh1.Range("C17") = "Yes" Or _
       sh1.Range("C18") = "Yes" Or _
       sh1.Range("C19") = "Yes" Or _
       sh1.Range("C20") = "Yes" Then
        sh2.Unprotect Password:="xxx"
        sh2.Range("D4") = "Payment Process"
        sh2.Protect Password:="xxx"
    End If

ElseIf sh1.Range("C10:C21") = "No" Then
    sh2.Unprotect Password:="xxx"
    sh2.Range("D4") = "Non-Payment"
    sh2.Protect Password:="xxx"
Else
    sh2.Unprotect Password:="xxx"
    sh2.Range("D4") = "Needs Review"
    sh2.Protect Password:="xxx"
End If
Sheets("Classifier").Select

End Sub

I fixed a few errant newlines and mixed _. Note, there's a better way to do your various Or statements. But first, can all those cells be Yes, or only one at a time, for you to want to unprotect and protect?

Edit: I think I cleaned it up a little, using COUNTIF(). See if this works:

Private Sub CommandButton2_Click()
Dim sh1     As Worksheet
Dim sh2     As Worksheet
Set sh1 = ActiveWorkbook.Sheets("Completed Questionnaire")
Set sh2 = ActiveWorkbook.Sheets("Classifier")

If WorksheetFunction.CountIf(sh1.Range("C10:C11", "C14"), "Yes") > 0 Then
    If WorksheetFunction.CountIf(sh1.Range("C19:C20", "C17"), "Yes") > 0 Then
        sh2.Unprotect Password:="xxx"
        sh2.Range("D4") = "PS1"
        sh2.Protect Password:="xxx"
    End If

ElseIf WorksheetFunction.CountIf(sh1.Range("C10:C11"), "Yes") > 0 And _
       WorksheetFunction.CountIf(sh1.Range("C14:C16", "C18"), "Yes") > 0 Then
    sh2.Unprotect Password:="xxx"
    sh2.Range("D4") = "PS2"
    sh2.Protect Password:="xxx"

ElseIf WorksheetFunction.CountIf(sh1.Range("C12, C21"), "Yes") > 0 Then
    If WorksheetFunction.CountIf(sh1.Range("C10:C11"), "No") > 0 Then
        sh2.Unprotect Password:="xxx"
        sh2.Range("D4") = "PS3"
        sh2.Protect Password:="xxx"
    End If

ElseIf sh1.Range("C13") = "Vendor Only" And WorksheetFunction.CountIf(sh1.Range("C10:C11"), "Yes") > 0 Then
    If WorksheetFunction.CountIf(sh1.Range("C14:C20"), "Yes") > 0 Then
        sh2.Unprotect Password:="xxx"
        sh2.Range("D4") = "Payment Process"
        sh2.Protect Password:="xxx"
    End If

ElseIf WorksheetFunction.CountIf(sh1.Range("C10:C21"), "No") > 0 Then
    sh2.Unprotect Password:="xxx"
    sh2.Range("D4") = "Non-Payment"
    sh2.Protect Password:="xxx"
Else
    sh2.Unprotect Password:="xxx"
    sh2.Range("D4") = "Needs Review"
    sh2.Protect Password:="xxx"
End If
Sheets("Classifier").Select

End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Batma... I mean Master Wayne, I'd love to hear how to improve all the Or statements... and all the cells can potentially be "Yes", It doesn't have to be one at a time, I just need to check the contents of each cell, and as long as one of them contains a "Yes" it will execute the unprotect.
@Mdurocher - See the edited add'l code. I think that may help. I don't think it's possible to do Range("A1:A2") = "Yes" to see if either (or both) cells in that range are a value. You should get a Type Mismatch error if I'm not mistaken. Therefore I used Countif().
3

Comparing multiple cells at once

Comparing multiple cell values to other value is failing, e.g.:
sh1.Range("C19:C20") = "Yes"
What are you trying to do here? Test that both C19 AND C20 are = "Yes"?
Then (god forbid me for the code i am to write) use 2 comparisons:
sh1.Range("C19") = "Yes" AND sh1.Range("C20") = "Yes"

Syntax error

There is a syntax error in the 4th main IF block:
ElseIf sh1.Range("C13") = "Vendor Only" And sh1.Range("C10:C11") = "Yes" Then _
Should be:
ElseIf sh1.Range("C13") = "Vendor Only" And sh1.Range("C10:C11") = "Yes" Then

And to apply what we learned above, it should really be:
ElseIf sh1.Range("C13") = "Vendor Only" And sh1.Range("C19") = "Yes" AND sh1.Range("C20") = "Yes" Then

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.