VBA抛出'else without if'错误

时间:2022-11-19 23:35:30

I was trying out the following code and its repeatedly throwing the 'else without if' and such similar errors. I just cant figure out where the error lies, because each if is properly terminated.

我正在尝试以下代码,并反复抛出'else if if'和类似的错误。我只是无法弄清楚错误在哪里,因为每个if都被正确终止了。

Here is the code:

这是代码:

Sub hra_macro()

Dim city As Boolean
Dim salary As Integer
Dim varHRA As Integer
Dim mimHRA As Integer
Dim maxHRA As Integer
Dim A As Integer
Dim B As Integer
Dim Rent As Integer

city = Range("b1").Value
salary = Range("b2").Value
Rent = Range("b3").Value

If city = True Then

varHRA = 0

    A = Rent - (salary / 10)
    B = salary * 0.5
    Do While varHRA < salary
        varHRA = varHRA + 1
            If (varHRA < A And varHRA < B) Then
            minHRA = varHRA
            End If
            If (A < varHRA And A < B) Then
            minHRA = A
            End If
            If (B < varHRA And B < A) Then
            minHRA = B
            End If
            If minHRA > maxHRA Then
            maxHRA = minHRA
            End If
    Exit Do
Else '<<<<<<<<<<<<<<<<<<<< PROBLEM AREA

varHRA = 0

    A = Rent - (salary / 10)
    B = salary * 0.4
    Do While varHRA < salary
        varHRA = varHRA + 1
        If (varHRA < A And varHRA < B) Then
        minHRA = varHRA
        End If
        If (A < varHRA And A < B) Then
        minHRA = A
        End If
        If (B < varHRA And B < A) Then
        minHRA = B
        End If
    If minHRA > maxHRA Then
    maxHRA = minHRA
    End If
    Exit Do
End If

Range("b4").Value = maxHRA

End Sub

2 个解决方案

#1


8  

The Ifs look okay to me at first glance, but I noticed that both Dos are missing the Loop.

乍一看,Ifs看起来还不错,但我注意到两个Dos都错过了Loop。

It should look like this:

它应该如下所示:

Do While varHRA < salary
    'Do Stuff
Loop

Exit Do is used to exit the loop before the condition behind the While is true, but you always need the Loop keyword.

Exit Do用于在While后面的条件为真之前退出循环,但是您始终需要Loop关键字。

If the problem still remains, please post the exact error message that you're getting.

如果问题仍然存在,请发布您收到的确切错误消息。


EDIT:

I just tried to reproduce your problem in VBA in MS Access (I don't have Excel installed on this machine, only Access):

我只是尝试在MS Access中的VBA中重现您的问题(我没有在此机器上安装Excel,只有Access):

Public Function Test()

    If 1 = 0 Then

        Do While 1 = 0

            Stop

        Exit Do

    Else

        Stop

    End If

End Function

This simple piece of code gives me the exact same error message that you got:

这段简单的代码给了我完全相同的错误消息:

Compile error: Else without If

编译错误:没有If的其他错误

When I replace the Exit Do by a Loop, the error goes away and the code compiles.

当我用循环替换Exit Do时,错误消失并且代码编译。

So, you should replace the Exit Do by Loop in your code.

因此,您应该在代码中替换Exit Do by Loop。

#2


2  

Looks like your using 'Exit Do' instead of 'Loop'. 'Exit Do' will exit the Do loop, is this the way you want this to behave?

看起来你使用'Exit Do'而不是'Loop'。 'Exit Do'将退出Do循环,这是你想要这样做的方式吗?

#1


8  

The Ifs look okay to me at first glance, but I noticed that both Dos are missing the Loop.

乍一看,Ifs看起来还不错,但我注意到两个Dos都错过了Loop。

It should look like this:

它应该如下所示:

Do While varHRA < salary
    'Do Stuff
Loop

Exit Do is used to exit the loop before the condition behind the While is true, but you always need the Loop keyword.

Exit Do用于在While后面的条件为真之前退出循环,但是您始终需要Loop关键字。

If the problem still remains, please post the exact error message that you're getting.

如果问题仍然存在,请发布您收到的确切错误消息。


EDIT:

I just tried to reproduce your problem in VBA in MS Access (I don't have Excel installed on this machine, only Access):

我只是尝试在MS Access中的VBA中重现您的问题(我没有在此机器上安装Excel,只有Access):

Public Function Test()

    If 1 = 0 Then

        Do While 1 = 0

            Stop

        Exit Do

    Else

        Stop

    End If

End Function

This simple piece of code gives me the exact same error message that you got:

这段简单的代码给了我完全相同的错误消息:

Compile error: Else without If

编译错误:没有If的其他错误

When I replace the Exit Do by a Loop, the error goes away and the code compiles.

当我用循环替换Exit Do时,错误消失并且代码编译。

So, you should replace the Exit Do by Loop in your code.

因此,您应该在代码中替换Exit Do by Loop。

#2


2  

Looks like your using 'Exit Do' instead of 'Loop'. 'Exit Do' will exit the Do loop, is this the way you want this to behave?

看起来你使用'Exit Do'而不是'Loop'。 'Exit Do'将退出Do循环,这是你想要这样做的方式吗?