Solved

Code for button cannot be used for other procedures

Posted on 2013-05-31
5
220 Views
Last Modified: 2013-06-03
I have code attached to a button

Private Sub PerfCompare_Click()

I want to use the same code for another process.  When I add PerfCompare to the other code - it says Sub or Function not defined.

I have copied the code into another Function but it is a duplicate of the code and it seems that it would be confusing to someone looking at the code unless they know what I have done.

Is there a way to use the PerfCompare_Click code for another procedure and if so how?

Thanks in advance.
0
Comment
Question by:leezac
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 39211505
Assuming that you're trying to access the button code from a different sheet or component, you may need to change the Private to Public (or omit the word Private) so the other module can see it.

For example, if the code for the button is in Sheet1 and you're trying to access it from Sheet2, making it public will allow Sheet2 to run that procedure.

Specifically, you should call the sub something like this:

Sheet1.PerfCompare_Click

Open in new window

0
 
LVL 14

Accepted Solution

by:
Faustulus earned 375 total points
ID: 39213932
It isn't a good idea to have Public procedures in any worksheet's code module. That is because these modules are basically class modules (as is the ThisWorkbook module). The better way is to place your Public sub in a standard code module. You may need to add this to your project. Its default name will be Module1 but you can change that.

Declare your procedure as Sub PerfCompare_Click (without the prefix "Private") in the standard code module and you can call it from any other module, including any class module or even with a keyboard shortcut, just by its name 'PerfCompare_Click'.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39214577
Faustulus, you make a valid point.  I rarely put any Public procedures inside my Sheet modules, mainly because it makes it harder to locate them when visually scanning around for them (like...where did I put that procedure again? Sheet1, Sheet2, Sheet3, thisWorkbook, oh yeah! Module1, where it should be!)  In this case, it seemed like a simple solution to the problem.  however, if there is more to it than that, it would be prudent to make a separate module and go forward from there.
0
 

Author Closing Comment

by:leezac
ID: 39216208
That helped a great deal.  I was able to change.
0
 
LVL 14

Expert Comment

by:Faustulus
ID: 39217672
Great!
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now