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.
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
The
Ifs look okay to me at first glance, but I noticed that bothDos are missing theLoop.It should look like this:
Exit Dois used to exit the loop before the condition behind theWhileis true, but you always need theLoopkeyword.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):
This simple piece of code gives me the exact same error message that you got:
When I replace the
Exit Doby aLoop, the error goes away and the code compiles.So, you should replace the
Exit DobyLoopin your code.