【发布时间】:2017-04-21 08:54:57
【问题描述】:
我再次需要你的帮助。 我正在尝试基于两个条件创建一个消息框。 我有两个列表: 一个从 1 到 20 的数字。 第二个是:单一股票、单一期权、一篮子股票和一篮子期权。
当在工作表“输入”单元格“F7”中选择单一股票/单一选项时,应该只能在单元格“F8”中使用数字“1”,否则应显示错误消息框。
当在工作表“输入”单元格“F7”中选择一篮子股票/期权时,只能在单元格“F8”中使用 >1 的数字,否则应显示错误消息框。
我已尝试使用此代码:
Sub Msg_exe()
If Target.Address = "$F$8" Then
If Target.Value > 2 Then
If Target.Address = "Stock" Then
If Target.Address = "Option" Then
MsgBox "Error!", vbExclamation, "Error"
End If
End If
End If
End If
Sub Msg_exe()
If Target.Address = "$F$8" Then
If Target.Value < 2 Then
If Target.Address = "Basket of Stocks" Then
If Target.Address = "Basket of Options" Then
MsgBox "Error!", vbExclamation, "Error"
End If
End If
End If
End If
【问题讨论】:
-
您发布的代码存在各种问题,首先您声明了两次不支持的子Msg_exe。其次,您引用 Target,但是在任何一个方法签名中都没有名为 Target 的变量。
-
.Address也永远不会返回像“Stock”这样的东西,即使它会,你检查两个不同的值,所以这永远不会做任何事情。 -
@Sorceri 如何找到解决方案?我没有使用 VBA 的经验,因此我将不胜感激
-
@arcadeprecinct 你能告诉我你会怎么做吗?
-
对工作表事件做一些研究并阅读一些关于 Excel VBA 的教程(
Range是 Excel VBA 中最基本和最重要的事情之一)
标签: vba if-statement macros conditional-statements messagebox