VBA return MsgBox 中有 2 个变体

VBA return 2 variants in MsgBox

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

当我运行这个宏时,Coefficient和Degree都没有出现在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

编辑:评论建议的新版本代码(仍然不起作用):

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。运行 该过程,您应该会看到预期的结果。您可以从那里开始构建。