"编译错误:否则没有"

时间:2015-09-11 17:02:50

标签: excel vba excel-vba math

我有错误的VBA宏没有意义(我之前遇到过问题并清楚地看到IF语句)。错误讯息:

  

编译错误:没有If

代码(开发中间值定理的演示):

Option Explicit
Sub Function1()
Dim Polynomial As String
Dim Sign
Dim counter As Integer
Dim Degree
Dim Coefficient

While Cells(counter + 1, 1).Value <> "" And Cells(counter + 1, 2).Value <> "" '+1 because top row occupied by column A, column B titles. If cell is empty stop.
MsgBox "Enter polynomial by terms."
Degree = Cells(counter + 1, 1).Value 'A2
Coefficient = Cells(counter + 1, 2).Value 'B2
If (Coefficient < 0) Then Sign = " - " ' if coefficient negative
    Else: If Coefficient > 0 Then Sign = " + " ' if coefficient positive
End If
Polynomial = Polynomial & " " & Coefficient & "x^" & Degree 'concatenation string, list polynomial.
counter = 1
counter = counter + 1
Wend
' Finally:
MsgBox poly
End Sub

2 个答案:

答案 0 :(得分:4)

更改为:

If (Coefficient < 0) Then 
    Sign = " - " ' if coefficient negative
Else
    If Coefficient > 0 Then Sign = " + " ' if coefficient positive
End If

答案 1 :(得分:3)

在After之后你需要一个新行。否则,if语句将在行尾自动关闭。 VBA抱怨说,当看到Else时,没有开放的阻止。