我正在创建VBA宏来演示中间值定理。用户输入多项式项的度数和系数。术语连接成多项式。
当我跑步时,我得到:
到目前为止运行时错误' 1004':
应用程序定义或对象定义的错误
代码。
Option Explicit
Sub Function1()
Dim Polynomial As String
Dim Sign
Dim RowCounter As Integer
Dim Degree
Dim Coefficient
Dim Term As String
Dim Variable
MsgBox "Enter polynomial by terms."
Variable = InputBox("Enter variable (x, y, z, etc.).")
Degree = Cells(RowCounter, 1).Value 'A2 and down
RowCounter = 1
While Cells(RowCounter, 1).Value <> "" And Cells(RowCounter, 2).Value <> "" ' as long as there is a coefficient and degree
If (Coefficient < 0) Then
Sign = " - " ' if coefficient negative
Else
If Coefficient > 0 Then Sign = " + " ' if coefficient positive
End If
Term = Sign & Coefficient & Variable & "^" & Degree = Cells(RowCounter + 1, 3).Value ' C2 and down
Polynomial = Polynomial & Cells(RowCounter, 3).Value
MsgBox Polynomial
RowCounter = RowCounter + 1
Wend
End Sub
答案 0 :(得分:2)
在Rowcounter
之前移动Degree = Cells(RowCounter, 1).Value
。否则,您将调用Cells(0,1),返回错误1004。
RowCounter = 1
Degree = Cells(RowCounter, 1).Value