Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6640491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:40:43+00:00 2026-05-25T23:40:43+00:00

I was trying out the following code and its repeatedly throwing the ‘else without

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:40:44+00:00Added an answer on May 25, 2026 at 11:40 pm

    The Ifs look okay to me at first glance, but I noticed that both Dos are missing the 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.

    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):

    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

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

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just trying to work out why the following code is leaking memory and
I was trying out the following code which actually saves the pdf file to
I was trying out JLayeredPane. So, in the following code, I created a JLayeredPane
I'm trying to use the following code and it still strips out all the
I have been trying to find out why the following lines of code do
Trying to figure out this pseudo code. The following is assumed.... I can only
I'm just trying out PHPUnit with CakePHP and i'm getting an the following error
I am trying to delete with the following code. [super deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; Its return
I am trying out the following piece of code to get access a JSF
Trying the following code doesn't seem to work out for me quite as planned:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.