VBA在MsgBox中返回2个变体

这是一个简单的math定理演示的开始。

当我运行这个macros没有系数或度出现在MsgBox。

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 

编辑:评论的build议的新版本的代码(仍然无法正常工作):

 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 

你可能想重写你的过程,就像这样:

 Sub Function1() Dim Degree, Coefficient MsgBox "Enter polynomial by terms." Degree = Cells(1, 2).Value Coefficient = Cells(2, 2).Value If Len(Trim(Degree)) > 0 And IsNumeric(Degree) Then MsgBox "Degree is numeric" Else MsgBox "Degree is not numeric. Exiting" Exit Sub End If If Len(Trim(Coefficient)) > 0 And IsNumeric(Coefficient) Then MsgBox "Coefficient is numeric" Else MsgBox "Coefficient is not numeric. Exiting" Exit Sub End If MsgBox Coefficient & "x^" & Degree End Sub 

在单元格B1中键入1.在单元格B2中键入2.运行该过程,您应该看到预期的结果。 你可以从那里build立。