VBA - To Create List Box, which determines what section of code to run

Posted on 2013-08-31
Medium Priority
Last Modified: 2013-09-05
Hello Experts,

Please bare with me as I try to explain... :)  And keep in mind, I basically only know how to copy & paste VBA code, and I can just barely slightly - customize code myself.

I am creating a macro to be run on an email, but if I continue the way I'm going, - I'll have many different macros.

Ultimately, I would like this...

1.)  Macro is run.
2.)  A userform opens, which contains a list box.
3.)  List box choices are: "Sample Request", "Product Return", "Quote Request".

If the user selects "Sample Request", I want the code to jump to my section of code for "Sample Request".  If the user selects "Product Return", I want the code to jump to the code for "Product Return".

I am hoping for someone to write some sample code, that will jump to sections of a code based on what is selected in a listbox on a userform.

I do know how to create a userform in Outlook, and I know how to add the listbox - but I have no clue how to code, or to get any values inside of the listbox, and I have no idea how to make the code execute certain sections depending on what is selected.

I HOPE that made sense!

Thank you in advance for your help!

~ Geekamo
Question by:Geekamo
  • 2
LVL 52

Accepted Solution

Martin Liss earned 2000 total points
ID: 39455191
I don't do Outlook programming but here is an Excel workbook that shows the techniques. When you open the workbook type Ctrl+z which will run the ShowUserForm macro. Then choose any of the three listbox items.

To see the code, click the Visual Basic button or type Alt+F11. Then double-click on Module1 to see the macros or double-click on UserForm1 and then double-click the form itsef to see the userform code which is simply this:
Private Sub ListBox1_Click()
    Select Case ListBox1.ListIndex
        Case 0
        Case 1
        Case 2
    End Select
End Sub

Private Sub UserForm_Initialize()
    ListBox1.AddItem "Sample Request"
    ListBox1.AddItem "Product Return"
    ListBox1.AddItem "Quote Request"
End Sub

Open in new window

The Initialize routine loads the listbox when the form opens and the ListBox1_Click code calls one of three macros.

Let me know if you need any other explanation or help.

Author Closing Comment

ID: 39468480
Thank you very much!

~ Geekamo
LVL 52

Expert Comment

by:Martin Liss
ID: 39468492
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I came across an unsolved Outlook issue and here is my solution.
Configure external lookups on for external mail flow on Exchange 2013 and Exchange 2016.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question