【发布时间】:2015-09-10 17:41:18
【问题描述】:
这是一个简单的数学定理演示的开始。
当我运行这个宏时,MsgBox 中没有出现 Coefficient 和 Degree。
Option Explicit
Sub Function1()
Dim Degree, Coefficient
MsgBox "Enter polynomial by terms."
Cells(1, 2).Value = Degree
Cells(2, 2).Value = Coefficient
If IsNumeric(Degree) = True Then
Else: MsgBox "IsNumeric(Degree)=False)"
If IsNumeric(Coefficient) = True Then
Else: MsgBox "IsNumeric(Coefficient)=False"
MsgBox Coefficient & "x^" & Degree
End If
End If
End Sub
编辑:cmets 建议的新版本代码(仍然不起作用):
Option Explicit
Sub Function1()
Dim Degree, Coefficient
MsgBox "Enter polynomial by terms."
Degree = Cells(1, 2).Value
Coefficient = Cells(2, 2).Value
If IsNumeric(Degree) = True Then
Else: MsgBox "IsNumeric(Degree)=False)"
If IsNumeric(Coefficient) = True Then
Else: MsgBox "IsNumeric(Coefficient)=False"
MsgBox Coefficient & "x^" & Degree
End If
End If
End Sub
【问题讨论】:
-
您永远不会为
Degree和Coefficient赋值。相反,您将单元格 B1 和 B2 分配给空值,因为从未为度数和系数分配值。这些线应该是相反的吗?这样Degree = Cells(1, 2).Value?? -
赋值顺序看起来不对,试试
Degree = Cells(1, 2).Value等等。 -
感谢您的建议。还是不行。
-
...别傻了,但您的单元格中有数据
B1 and B2是吗?您是否在使用多个工作表/工作簿?B1和B2的值是多少?如果您单步执行宏(按F8),在Degree = Cells(...行之后,将鼠标悬停在“Degree”上 - 它显示什么?它应该弹出B2中的值。查看您编辑的代码,如果您的Degree未设置,那么您如何获得消息框?我认为If IsNumeric(Degree)将是FALSE,因此不应运行If语句,因此不会显示任何消息框? -
你的外部
If语句结构错误,所有代码都放在Else部分
标签: vba excel math office-2013