on click events in excel

i have a calendar that i would like to reduce the lines of code for the onclick event similar to ms access onclick events eg,
Private Sub D1_Click(): Call BUTTON_CLICK("D1"): End Sub

for i=1 to 42
'the onclick procedure here to replace the private sub above

Who is Participating?
andrewssd3Connect With a Mentor Commented:
I think you are asking about having a common error handler for multiple buttons, although you could be more forthcoming about your requirements.  What you want is not completely simple, but you can achieve it reasonable easily if you understand the principles.

Take a look at the attached example - you shold be able to adapt it to what you want.

You could also look at some articles, for example http://www.cpearson.com/excel/Events.aspx
Calvin BrineCommented:
A couple of clairifications.

1.  Is your calendar an Excel Userform?  An ActiveX Control?  Is it embbed in your worksheet?
2.  I'm not sure what you are trying to accomplish with your loop?  Are you trying to call the button_click event 42 times?  

If you can provide some context on what you are trying to accomplish, we might be able to provide with what you want, or even something that might be even easier that you may not have thought of.

SvgmassiveAuthor Commented:
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Calvin BrineCommented:
SvgmassiveAuthor Commented:
this is what's in code.i can eliminate this with a loop using the onclick event similar to ms access
Private Sub D1_Click(): Call BUTTON_CLICK("D1"): End Sub
Private Sub D2_Click(): Call BUTTON_CLICK("D2"): End Sub
Private Sub D3_Click(): Call BUTTON_CLICK("D3"): End Sub
Private Sub D4_Click(): Call BUTTON_CLICK("D4"): End Sub
Private Sub D5_Click(): Call BUTTON_CLICK("D5"): End Sub
I don't understand what you're asking.  I have given you a working and tested example of how to do this flexibly.  You can do what you're suggesting if you really want to repeat the code 42 times.  If you want to do it with repetitive code like you suggest you could just have a trivial routine like
Sub Button_Click(byval sBtn as string)
            MsgBox "You clicked " & sBtn
End Sub

Open in new window

But I would imagine you could have worked this out by yourself.
SvgmassiveAuthor Commented:
i used th application caller method thanks
SvgmassiveAuthor Commented:
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.

All Courses

From novice to tech pro — start learning today.