Run macro when combo box changes

Thomas

Active Member
Joined
May 3, 2002
Messages
366
Heya everyone... can't seem to get this one:

I have a worksheet that I've added a combobox to that allows users the ability to change the data that is shown. I need to run a macro when the user selects a different value in the combobox,, but cannot seem to get it to fire off.

I've named the combobox 'RepComboBox' and tried adding the code

Code:
Private Sub RepComboBox_Change()
       RunMyMacro
End Sub

but nothing happens. Any thoughts?


PS... if it matters, I'm on Excel 2007.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try

Code:
Private Sub RepComboBox_Change()
Run MyMacro
End Sub

Or

Code:
Private Sub RepComboBox_Change()
Call MyMacro
End Sub
 
Upvote 0
Hmm... unfortunately, these doesn't seem to trigger the code.

If I manually run:
Code:
Private Sub RepComboBox_Change()
Run MyMacro
End Sub

I get an error for 'expected function or variable'

If I manually run:
Code:
Private Sub RepComboBox_Change()
Call MyMacro
End Sub

It calls the function like it should, but the RepComboBox doesn't trigger it.

I've even tried using a Worksheet_Change() event looking at the linked cell for the ComboBox, but still no dice.
 
Upvote 0
Well I can't see why, what is MyMacro please
 
Upvote 0
Here's what I have:

In module for the worksheet "Dashboard":
Code:
Private Sub RepComboBox_Change()
     Call ChartLimits
End Sub

In its own module (truncated version):
Code:
Sub ChartLimits()
    Sheets("Dashboard").Activate
    
    If ThisWorkbook.Names("RepChoice").RefersToRange = "Summary - All" Then
        ActiveSheet.ChartObjects("Chart 4").Select
        ActiveChart.Axes(xlValue).MaximumScale = 25000
        ActiveChart.Axes(xlValue).MajorUnit = 12500
        
        ActiveSheet.ChartObjects("Chart 5").Select
        ActiveChart.Axes(xlValue).MaximumScale = 25000
        ActiveChart.Axes(xlValue).MajorUnit = 12500
        ActiveChart.SetElement (msoElementPrimaryValueAxisNone)
    Else
        ActiveSheet.ChartObjects("Chart 4").Select
        ActiveChart.Axes(xlValue).MaximumScale = 5000
        ActiveChart.Axes(xlValue).MajorUnit = 2500
        
        ActiveSheet.ChartObjects("Chart 5").Select
        ActiveChart.Axes(xlValue).MaximumScale = 5000
        ActiveChart.Axes(xlValue).MajorUnit = 2500
        ActiveChart.SetElement (msoElementPrimaryValueAxisNone)
    End If

    Sheets("Dashboard").Range("A1").Select
End Sub

To explain: the named range in the book 'RepChoice' is an offset of the list populating the ComboBox, and its value is equal to that of the user's choice in the box (as opposed to the numerical value output by the ComboBox itself); when "Summary - All" is selected, the chart limits are raised; otherwise they're lowered.

So far, I haven't been able to get the event to trigger.
 
Upvote 0
***SOLVED***

...not exactly what I was expecting to do, but I happened to right-click the control and noticed an 'Assign Macro' option; did that and it works a treat!

Never could get an event to fire; still don't understand why, but I guess it doesn't matter if it works.

Thanks anyway for the help!
 
Upvote 0
Right there are two comboboxes in Excel, one comes from the Forms controls and the other from the controls (or ActiveX) controls. The fact that you needed to assign a macro means you chose the one from the Forms controls. If you use the other one and right click you will not see 'assign macro' but 'view code' instead.

Point to note for future reference ;)
 
Upvote 0
This thread is very old but I have found it extremely helpful. Thanks
 
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