VBA: Select Month from ListBox1 and ButtonClick to Run Macro

Joined
Dec 8, 2021
Messages
26
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hello I need assistance. I cannot for the life of me figure out how to get this to work

fig 1: shows my empty table and this same Worksheet ("Worksheet 1" we will call it) I have ListBox1 populated with each month and CommandButton1 which is to be clicked after selecting a month from ListBox1:
1665956446825.png


Each month has a separate macro tied to it for example, If the user selects "January" and clicks "Launch Report", its associated macro "RunJan" will trigger and populate the table with data from my January sheet. I have the RunJan macro figured out.

I can't figure out how to properly write a the trigger for each macro depending on the ListBox1 selection.

Currently I have the following code nested in Worksheet1 with just RunJan macro.

Private Sub Workbook_Open()

VBA Code:
With Worksheets("Worksheet1").ListBox1

ListBox1.AddItem " "
ListBox1.AddItem "January"
ListBox1.AddItem "February"
ListBox1.AddItem "March"
ListBox1.AddItem "April"
ListBox1.AddItem "May"
ListBox1.AddItem "June"
ListBox1.AddItem "July"
ListBox1.AddItem "August"
ListBox1.AddItem "September"
ListBox1.AddItem "October"
ListBox1.AddItem "November"
ListBox1.AddItem "December"
End With
End Sub

Private Sub LaunchReport_Click()

If ListBox1.Value = "January" Then
Call RunJan

End If
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Unless I am missing something, and just taking the second part of your posted code because if you keep running the top half, your listbox is going to get very long with duplicate entries.
Why wouldn't this work... This code would be put in the Worksheet1 module.

VBA Code:
Private Sub LaunchReport_Click()

    If Me.ListBox1.Value = "January" Then
        Call RunJan
    End If
    If Me.ListBox1.Value = "February" Then
        Call RunFeb
    End If
    If Me.ListBox1.Value = "March" Then
        Call RunMar
    End If
    'Etc copy and paste for the rest of the months...

End Sub
 
Upvote 0
Unless I am missing something, and just taking the second part of your posted code because if you keep running the top half, your listbox is going to get very long with duplicate entries.
Why wouldn't this work... This code would be put in the Worksheet1 module.

VBA Code:
Private Sub LaunchReport_Click()

    If Me.ListBox1.Value = "January" Then
        Call RunJan
    End If
    If Me.ListBox1.Value = "February" Then
        Call RunFeb
    End If
    If Me.ListBox1.Value = "March" Then
        Call RunMar
    End If
    'Etc copy and paste for the rest of the months...

End Sub
hello @igold I removed the "additems"code and put in a separate module, I was a little confused as to how to populate the Listbox, any pointers on that by chance? The code you provided didn't set off the macro. Let me paste my macro code here in case thats the problem. I know it works by itself. Perhaps its due to it pulling from multiple sheets in the work book? Should I be writing a Loop instead?
Here is the macro that the button should be setting off. Please replace "Worksheet 1" with ""FYI and Error Report":

VBA Code:
Sub RunJan()

Application.Goto Reference:="JanReport"
    Selection.Copy
    Sheets("FYI and Error Report").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
    Sheets("FYI and Error Report").Select
   End Sub
Sub runFeb()

Application.Goto Reference:="FebReport"
    Selection.Copy
    Sheets("FYI and Error Report").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
    Sheets("FYI and Error Report").Select
   End Sub
 
Upvote 0
This code should stay in the "ThisWorkbook" module...
VBA Code:
Private Sub Workbook_Open()

    With Worksheets("Worksheet1").ListBox1
        ListBox1.AddItem " "
        ListBox1.AddItem "January"
        ListBox1.AddItem "February"
        ListBox1.AddItem "March"
        ListBox1.AddItem "April"
        ListBox1.AddItem "May"
        ListBox1.AddItem "June"
        ListBox1.AddItem "July"
        ListBox1.AddItem "August"
        ListBox1.AddItem "September"
        ListBox1.AddItem "October"
        ListBox1.AddItem "November"
        ListBox1.AddItem "December"
    End With
    
End Sub

and then my code in Post #2 should go in the worksheet module where your "Launch Report" button and Listbox are located. The actual subs themselves (RunJan, RunFeb, etc) should go in a plain code module...
 
Upvote 0
I followed your directions but this is what it is happening. For populating the ListBox I have to input it without ListBox1 stated at the beginning of each line:

VBA Code:
Private Sub Workbook_Open()

    With Sheets("FYI and Error Report").ListBox1
    
        .AddItem " "
        .AddItem "January"
        .AddItem "February"
        .AddItem "March"
        .AddItem "April"
        .AddItem "May"
        .AddItem "June"
        .AddItem "July"
        .AddItem "August"
        .AddItem "September"
        .AddItem "October"
        .AddItem "November"
        .AddItem "December"
        
    End With
I plugged in the original code you provided me as directed into the worksheet module where the button is and nothing happpens when I click the button. I am dumbfounded
 
Upvote 0
I am not sure I understand your post. The code in Post #4, has to go into the "ThisWorkbook" module. By it's nature, every time the workbook is opened that code will trigger and populate your ListBox1, provided that the sheet names are correct as taken from your OP.

Is the name of the button "LaunchReport". If it is then that code should fire when you click the button as long as it is placed in the module as specified.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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