473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

How to Make 30 cmdButtons Run Same Code Without Adding Code to Each One

I have 30 CommandButtons on a UserForm in VBA Excel. When anyone of these buttons gets clicked, I want it to run the same function (See below).

How can I accomplish this without adding the function to each CommandButton like below?

Thanks very much for your suggestions.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandButton1_Click()
  2.     Call MyFunction
  3. End Sub
  4.  
  5. Private Sub CommandButton2_Click()
  6.     Call MyFunction
  7. End Sub
  8.  
  9. ..........
  10. ..........
  11. ..........
  12.  
  13. Private Sub CommandButton30_Click()
  14.     Call MyFunction
  15. End Sub
Jul 31 '07 #1
1 1121
Killer42
8,435 Expert 8TB
To be honest, I don't think you can.

In VB6 (and earlier versions), it's perfectly simple. You just create them as a control array, and they all share the same event procedures. It's wonderful.

For unfathomable reasons, M$ have apparently decided to remove this functionality from VBA and later (.Net) versions of VB. There are ways to simulate a control array in your code, but I don't know of any way to get around the need for individual event procedures like yours.

One suggestion. It might be a good idea to pass the control name to MyFunction. Just in case you ever need to know which button was clicked. In fact, I suspect the simplest thing would be to pass the actual button control to the function. That way, you can do whatever you like with it.
Jul 31 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: madsgormlarsen | last post by:
Hi I need to make at mulitlingual website, with php amd mysql, and I am looking for tutorils or books that explains about how to best do this. Hop ypu have some suggestions as to were I can find...
10
by: sp0 | last post by:
Is there a reason why to make mix numbers improper when adding? It seems when subtracting and adding, adding a subtracting the whole numbers and fraction parts should be sufficient? what'ch think
3
by: Susan Bricker | last post by:
Greetings. I have three forms that are open at the same time. They are related and cascading. The first form (frmEventAdd) is the anchor. Each event can have many Trials. The second form is...
8
by: Ignazio | last post by:
When creating forms with Visual C++ 2005, all the code for building the interface (the InitializeComponent method) and event handlers are set in the ..H file, as they were inline methods. So I ask,...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
7
by: eselk | last post by:
I'm doing some speed tests. I created a brand-new table, with just one "Long Integer" field. I'm testing adding 1000 records. If I use the "Export" feature in Access, it takes only a few seconds...
1
by: Pugi! | last post by:
Because my functions tend to become rather lengthy I split up the functions for each subject or action in a function (a) that checks user input (filter and validation) and if this checks out ok it...
42
by: lorlarz | last post by:
Contrary to what one authority in the JavaScript field says: JavaScript does make errors when dealing with just with integers. This authority (Douglas Crockford.) says: "integer arithmetic in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.