Excelmacros是没有消息框,是和不同的方向

用户在YesNo Message Box中有两个选项。 如果否,则执行特定的filter序列,但如果用户对消息框问题回答“是”,则要过滤另一列。 目前,在“Else”,我得到一个错误,如下所示:“编译错误:赋值左侧的函数调用必须返回Variant或Object”如果我取出“Else”,并且之后的代码,macros运行平稳,但只有当用户select否

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 

我也build议不要使用ActiveSheet除非你确定这就是你想要的。