Multiple choices falls into 4 possible sections and each section calls a differ sub procedure? If and Else Statement not working

Oberon70

Board Regular
Joined
Jan 21, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am hoping to get help as I am new to VBA and I am a bit lost on how to arrange a choice to initiate a specific Sub Procedure.

I have written code with an input box and a start of an It and Else Statement, but I don't know how I can get it to work when there are four groups. This code will be changed, where I obtain a portfolio code from a statement this code will fall into four different groups, which depends on what system the files are on.

I have four systems below are fake Portfolio Codes

WayneRC,WayneT2E,WayneIAE,WayneT15E goes to system 1.
WayneIA,WayneMCIA goes to system 2.
WayneIAG,WayneRCG,WayneT2G,WayneT15G goes to system 3.
Waynei90 goes to system 4

When the code sees any of the Portfolio codes for system one it will run Call System1_Sort
and the same for system 2, system 3 and system 4.

Call System2_Sort
Call System3_Sort
Call System4_Sort

VBA Code:
Sub Main()

Dim PortfolioCode As Variant

With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
    .CutCopyMode = False
End With

PortfolioCode = InputBox("Enter the Portfolio Code")

If PortfolioCode = "WayneRC" Or "WayneT2E" Or "WayneT15E" Or "WayneIAE" Then
    Call System_1
Else
Call System_2
End If

With Application
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    .EnableEvents = True
    .CutCopyMode = True
End With

End Sub

Sub System_1()
MsgBox "You have chosen System 1"
End Sub

Sub System_2()
MsgBox "You have chosen System 2"
End Sub
Sub System_3()
MsgBox "You have chosen System 3"
End Sub

I did try to use a mini sheet, but the only thing on the sheet is a button to run the code.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about
VBA Code:
Sub Main()

Dim PortfolioCode As Variant

With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
    .CutCopyMode = False
End With

PortfolioCode = InputBox("Enter the Portfolio Code")

Select Case PortfolioCode
   Case "WayneRC", "WayneT2E", "WayneT15E", "WayneIAE"
      Call System_1
   Case "WayneIA", "WayneMCIA"
      Call System_2
   Case "WayneIAG", "WayneRCG", "WayneT2G", "WayneT15G"
      Call System_3
   Case "Waynei90"
      Call System_4
   Case Else
      MsgBox "Invalid code"
End Select

With Application
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    .EnableEvents = True
    .CutCopyMode = True
End With

End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub Main()

Dim PortfolioCode As Variant

With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
    .CutCopyMode = False
End With

PortfolioCode = InputBox("Enter the Portfolio Code")

Select Case PortfolioCode
   Case "WayneRC", "WayneT2E", "WayneT15E", "WayneIAE"
      Call System_1
   Case "WayneIA", "WayneMCIA"
      Call System_2
   Case "WayneIAG", "WayneRCG", "WayneT2G", "WayneT15G"
      Call System_3
   Case "Waynei90"
      Call System_4
   Case Else
      MsgBox "Invalid code"
End Select

With Application
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    .EnableEvents = True
    .CutCopyMode = True
End With

End Sub
Wow, thanks. I am going to try it out and also read up case.
 
Upvote 0
The code worked perfectly:), I changed it to work with the code it received from a statement and did exactly what I wanted:) - thank you.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,174
Members
449,071
Latest member
cdnMech

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