我一直在收到'不匹配的类型错误',或者其他一些琐事错误,例如'no block if if'。然而,当我修复一个,另一个弹出备份。不确定此代码中的问题所在。它非常直接,获取单元格值,然后根据值进行分类。
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
答案 0 :(得分:4)
试试这个:
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
我修复了一些错误的换行符并混合_
。请注意,有一种更好的方法可以执行各种Or
语句。但首先,所有这些单元格是Yes
,还是一次只能有一个单元格,以便您想要取消保护和保护?
编辑:我想我使用COUNTIF()
清理了一下。看看是否有效:
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
答案 1 :(得分:3)
将多个单元格值与其他值进行比较失败,例如:
sh1.Range("C19:C20") = "Yes"
你想在这做什么?测试C19和C20是否=“是”?
然后(上帝禁止我写我要编写的代码)使用2个比较:
sh1.Range("C19") = "Yes" AND sh1.Range("C20") = "Yes"
第4个主IF块中存在语法错误:
ElseIf sh1.Range("C13") = "Vendor Only" And sh1.Range("C10:C11") = "Yes"
Then _
应该是:
ElseIf sh1.Range("C13") = "Vendor Only" And sh1.Range("C10:C11") = "Yes" Then
要应用我们上面学到的东西,它应该是:
ElseIf sh1.Range("C13") = "Vendor Only" And sh1.Range("C19") = "Yes" AND sh1.Range("C20") = "Yes" Then