If/Else/If/ElseIf - avoid repeating code

fastballfreddy

Board Regular
Joined
Jan 13, 2015
Messages
64
Office Version
  1. 2016
Platform
  1. Windows
I have a code that works but I'm repeating part of my code 3 times (Action 2 below in the code) and want to see if there is a way to only have it once. My current code uses - If, Else, If, ElseIf to process.

The 3 scenarios are the outcomes that could happen. Is there a way to always run Action 2 at the end without repeating it 3 times?
  • Action 1 & Action 2
  • Action 3, Action 4 & Action 2
  • Action 3, Action 5 & Action 2

VBA Code:
Sub New Game()
IF CONDITION THEN
    ACTION 1 & ACTION 2 (WOULD LIKE TO BE RAN AFTER EVERYTHING REGARDLESS)

ELSE ACTION 3

IF CONDITION THEN
    ACTION 4 & ACTION 2 (WOULD LIKE TO BE RAN AFTER EVERYTHING REGARDLESS)

ELSEIF CONDITION THEN
    ACTION 5 & ACTION 2 (WOULD LIKE TO BE RAN AFTER EVERYTHING REGARDLESS)

End If
End If
End If
 
May be,
VBA Code:
Sub NewGame()
    If Condition Then
        Action 1
        Action 2
    Else
        Action 3
    End If
    If Condition Then
        Action 4
    ElseIf Condition Then
        Action 5
    End If
    Action 2
End Sub
 
Upvote 0
Your pseudo code doesn't match the scenarios and actions you described.

Are the three conditions cumulatively exhaustive? That is, is it possible for all of the conditions be FALSE? If so, yeah, you have to repeat it every time.

Another approach is
VBA Code:
If Condition1 Then
   Action1
End If

If Condition1 Or Condition2 Or Condition3 Then
   Action2
End If

If Condition2 Or Condition3 Then
   Action3
End If

If Condition2 Then
   Action4
End If

If Condition3 Then
   Action5
End If
 
Upvote 0
Solution
I have a code that works but I'm repeating part of my code 3 times (Action 2 below in the code) and want to see if there is a way to only have it once. My current code uses - If, Else, If, ElseIf to process.

The 3 scenarios are the outcomes that could happen. Is there a way to always run Action 2 at the end without repeating it 3 times?
  • Action 1 & Action 2
  • Action 3, Action 4 & Action 2
  • Action 3, Action 5 & Action 2

VBA Code:
Sub New Game()
IF CONDITION THEN
    ACTION 1 & ACTION 2 (WOULD LIKE TO BE RAN AFTER EVERYTHING REGARDLESS)

ELSE ACTION 3

IF CONDITION THEN
    ACTION 4 & ACTION 2 (WOULD LIKE TO BE RAN AFTER EVERYTHING REGARDLESS)

ELSEIF CONDITION THEN
    ACTION 5 & ACTION 2 (WOULD LIKE TO BE RAN AFTER EVERYTHING REGARDLESS)

End If
End If
End If
Why don't you use arrays and key-value operators in complex logical structures instead of repeating If statements multiple times?
 
Upvote 0

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