Link to home
Start Free TrialLog in
Avatar of Mattie Owens
Mattie Owens

asked on

Running maco from dropdown list

I am trying to incorporate the following code concept into a worksheet.  I have a dropdown list staring at Cell E1. What I'm trying to do is if I select a value from the dropdown list that a macro is ran depending on the value.

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = True

    If Not Intersect(Target, Range("E1")) Is Nothing Then
        Select Case Range("E1")
       
            Case "Testing Value 1"
                Macro1
            Case "Testing Value 2"
                Macro2 ""
            Case "Testing Value 3"
                Macro3
        End Select
    End If
    End Sub
Sub Macro1()
MsgBox "Here it is 1"
End Sub
Sub Macro2()
MsgBox "Here it is 2"
End Sub
Sub Macro3()
MsgBox "Here it is 3"
End Sub
Avatar of Norie
Norie

That code looks fine to me, is it not working as expected?
Where did you place the Sheet Change Event code? It should be on Sheet Module not on a Standard Module.

Place a BreakPoint on the line If Not Intersect(Target, Range("E1")) Is Nothing Then and then change the value of E1 and once if the code stops on that line, press F8 key to run each line manually to check how it runs.
Avatar of Mattie Owens

ASKER

I have the code in the sheet module, and when I click the dropdown and select a value nothing happens.  I can go to the code and it runs successfully, however if I change the code on the worksheet itself nothing. See the attached file.
The code for the macros should probably be in standard modules, try moving it.

P.S. No attachment I'm afraid.
I tried putting the code in a module with no results. I've now attached the sample file.
Run-macro-based-on-value.xlsm
When I try to open that file Excel tells me that the file format is invalid.
Martin Liss your file does work, but only once. For some reason if you try to trigger it again after awhile it stops working.
Let me know if this is better.
29117951a.xlsm
Hi Martin, when I opened your second file and click the drop down box it worked fine. However, it will not work again. I even closed out the file and re opened and it did not work. Not sure whats up with that. But, I guess have to take another approach. Just seems strange.
It works for me. Try completely exiting Excel.
2018-09-14_16-28-09.mp4
When the code doesn't work, open VB Editor and press Ctrl + G to open Immediate Window and in there type ?Application.EnableEvents and hit Enter. What do you get? True or False?
Subodh

If I just type in the words Application.EnableEvents  and hit enter I get Compile Error Invalid Use of Property
Application.EnableEvents = True

or to find out what it is set to type ?Application.EnableEvents including the '?' in the immediate window and press Return. If it is False then type Application.EnableEvents = True in the immediate window and press Return
Hi Subodh

It returned true

 ?Application.EnableEvents
True
OK so are you still having the problem where it works a few times and then stops working? If so have you shut down Excel and then reopened it?
Yes I shut it down. Last night it wasn't working. This morning when I opened the file it was working. Now its not working again. Too random. I probably need to scrap. Just curiosity now on why its only works sometimes.
When you say "it" are you referring to my 29117951a.xlsm workbook? If so did you modify it in any way?
Your modification 29117951a.xlsm workbook does not work right now.
I made one small change. If this doesn't work for you then I'll give up.
29117951b.xlsm
What do the macros you are trying to run actually do?
The macro that I'm using opens a workbook. The macro on its own works fine.
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So it seems that what was happening is that even though I had the Application.EnableEvents = True, and when I checked in the intermediate window it indicated true, it would then go to false.

It was defaulting to Application.EnableEvents = False. I reviewed my code and there was nothing that seemed wrong. After doing some more net research I saw that it could be add-in issue. So I deleted a personal add-in and it now seems to be working. I'm going to wait awhile and try again to make sure that it is working and close out the question.

Thanks  Martin and Subodh
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I was able to get to the desired results with the help of both Martin Liss and Subodh Tiwari.  Thanks a lot.
You're welcome!
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
              Experts Exchange Top Expert VBA (current)