VBA Else statement is not working

Sun11

New Member
Joined
Nov 3, 2014
Messages
33
Hi Members,

I have this following code where, I tested it in all way, I feel the Else part of the coding is not working. Please help me to highlight where is the problem in this code. Why it does not coming in Else part.

Regards
Sun.

Code:
STEP1 = Calculation
If (condition) Then STEP2 = STEP1 - 1 Else STEP2 = STEP1
STEP3 = Calculation
STEP4 = Calculation
STEP5 = Calculation
STEP6 = Calculation
If (condition) Then
Y1 = Calculation
Y2 = Calculation
Y3 = Calculation
Y4 = Calculation
STEP7 = Y4
Else
STEP7 = Calculation


End If
Result = STEP7
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
There are 2 Else's so which one?

If you step through the code using F8, you can clearly see:
1. If condition is True or False
2. If the Else step is skipped.

Otherwise please explain what is actually causing your feeling the Else part of the coding is not working.
 
Last edited:
Upvote 0
Hello, Thank you for your reply, I notice that I didn't close 1st if else part before STEP3. So I reconstructed it and added End If. But its still not working, 2nd else part is not working. I tested control is not coming at 2nd else part.
 
Upvote 0
Hello,

This is a prime example of where code layout can be used to your advantage by increasing legibility. is this what your code should look like? 2 separate if statements?

Code:
Sub IFStatements()


STEP1 = Calculation


If (Condition) Then
    STEP2 = STEP1 - 1
Else
    STEP2 = STEP1
    STEP3 = Calculation
    STEP4 = Calculation
    STEP5 = Calculation
    STEP6 = Calculation
End If


If (Condition) Then
    Y1 = Calculation
    Y2 = Calculation
    Y3 = Calculation
    Y4 = Calculation
    STEP7 = Y4
Else
    STEP7 = Calculation
End If


Result = STEP7


End Sub
 
Upvote 0
Hi,I recoded it same way, but its still not working. Here is my updated code: (please note its a function & 1st IF is ending before the Step3)
Code:
Function Result(Data As Double)


Dim STEP1 As Double
Dim STEP2 As Double
Dim STEP3 As Double
Dim STEP4 As Double
Dim STEP5 As Double
Dim STEP6 As Double
Dim STEP7 As Double
Dim Y1 As Double
Dim Y2 As Double
Dim Y3 As Double
Dim Y4 As Double


STEP1 = Calculation
If (condition) Then
STEP2 = STEP1 - 1

Else
STEP2 = STEP1
End If


STEP3 = Calculation
STEP4 = Calculation
STEP5 = Calculation
STEP6 = Calculation
If (Condition) Then
Y1 = Calculation
Y2 = Calculation
Y3 = Calculation
Y4 = Calculation
STEP7 = Y4 
Else
STEP7 = Calculation
End If
Result = STEP7


End Function
 
Last edited:
Upvote 0
When second IF condition satisfies, STEP7= Y4 it is working. But STEP7 = Calculation not working. Even if I print a simple string, its not even printing. So obvious its control is not going to the 2nd else part.
Also upto STEP6 everything is working fine.

Please help..:confused:
 
Upvote 0
Please take a look at the former suggestions. Apparently you didn't pck up the layout suggestion in post #4. And what did you learn from stepping through the code using F8 (post #2)?

In any case, if the last condition = False, then the line STEP7 = Calculation must be executed.

Otherwise: can you share the details of "condition" and "calculation"?
Note we don't know anything about your specific situation, so your statement that something is not printing is meaningless.
The only proof is stepping through the code with F8 and observe that the line STEP7 = Calculation is skipped indeed, which for me is hard to imagine if the last condition is false.
 
Upvote 0
Please take a look at the former suggestions. Apparently you didn't pck up the layout suggestion in post #4. And what did you learn from stepping through the code using F8 (post #2)?

In any case, if the last condition = False, then the line STEP7 = Calculation must be executed.

Otherwise: can you share the details of "condition" and "calculation"?
Note we don't know anything about your specific situation, so your statement that something is not printing is meaningless.
The only proof is stepping through the code with F8 and observe that the line STEP7 = Calculation is skipped indeed, which for me is hard to imagine if the last condition is false.

Hi Sorry to point out that, F8 is not working with my excel 2010. When I put the curser on the in line of code it & press F8, nothing happens except a sound. I run this same code in other programming languages & it worked so there is no problem with calculations, only problem is the syntax here. But there is no debugging error in the code.

As I mentioned its function so still does it required to add sub? (I am coding VBA 1st time please keep patience with me)
Otherwise I followed code from post 4 on post 5.

At STEP3 we are calculating this part with the data for STEP2 which we tested with IF before. First IF part is got finished before STEP3. Then we keep calculating upto STEP6. Then we test STEP6 & if conditions is TRUE Then we calculate & finally STEP7 = Y4 (This part is working) IF conditions is FALSE ELSE calculate STEP7 separately. (This part is not working show wrong data)

Reposting the code again sharing the conditions:
Code:
Function Result(Data As Double)

Dim STEP1 As Double
Dim STEP2 As Double
Dim STEP3 As Double
Dim STEP4 As Double
Dim STEP5 As Double
Dim STEP6 As Double
Dim STEP7 As Double
Dim Y1 As Double
Dim Y2 As Double
Dim Y3 As Double
Dim Y4 As Double


STEP1 = Calculation
If ((STEP1 Mod 2) = 0) Then
    STEP2 = STEP1 - 1
Else
    STEP2 = STEP1
End If


STEP3 = Calculation
STEP4 = Calculation
STEP5 = Calculation
STEP6 = Calculation
If (STEP4 > Data & STEP6 = 0) Then
    Y1 = Calculation
    Y2 = Calculation
    Y3 = Calculation
    Y4 = Calculation
    STEP7 = Y4
Else
   STEP7 = Round(Abs(Data - STEP4) / STEP3)
End If
    Result = STEP7


End Function

Regards
 
Last edited:
Upvote 0
Hi Sorry to point out that, F8 is not working with my excel 2010. When I put the curser on the in line of code it & press F8, nothing happens except a sound. I run this same code in other programming languages & it worked so there is no problem with calculations, only problem is the syntax here. But there is no debugging error in the code.

As I mentioned its function so still does it required to add sub? (I am coding VBA 1st time please keep patience with me)

F8 is working, but you have to start it in a calling procedure (either another Function or a Sub). This is due to the required parameter Data As Double.

Please note Marcel's comment. "&" is for concatenating; And is what you want.

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top