Solved

Code for button cannot be used for other procedures

Posted on 2013-05-31
5
231 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

803 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