Excel Macro Yesno消息框,是的不同指示和否



用户在yesno消息框中有两个选项。如果否,它将执行一定的过滤器序列,但是如果用户回答"消息"框问题,我想过滤另一列。当前,在" else"中,我会收到一个读取"编译错误的错误:分配左侧的函数呼叫必须返回变体或对象",如果我取出" else",而在此之后的代码,则宏运行顺利,但仅在用户选择否时过滤。

If MsgBox("Is This Item Catch Weight?", vbYesNo) = vbNo Then
    retval = InputBox("Please Enter PO Cost")
    ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=71,         Criteria1:="=" & retval
    retval = InputBox("Please Enter Net Weight")
    ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=41, Criteria1:="=" & retval
Else: MsgBox("Is This Item Catch Weight?", vbYesNo) = vbYes
    retval = InputBox("Please Enter PO Cost")
    ActiveSheet.Range("$A$1:$CL$293662").AutoFilter Field:=71, Criteria1:="=" & retval
End If
End If

这里发生了一些事情。 : vba代码中的新行字符,因此

之类的行
Else: MsgBox("Is This Item Catch Weight?", vbYesNo) = vbYes

实际上与

相同
Else
   MsgBox("Is This Item Catch Weight?", vbYesNo) = vbYes

这不是您想要的。

最后还有一个额外的End If。删除。

调用消息框多次出现,可能也不是OYU想要的。您可能想显示消息框,获取结果然后对此进行操作。

Dim response As VbMsgBoxResult
response = MsgBox("Is This Item Catch Weight?", vbYesNo)
If response = vbNo Then
    'do stuff for yes
ElseIf response = vbYes Then
    ' do stuff for No
End If

我也建议不要使用ActiveSheet,除非您确定这是您想要的。

最新更新