Solved

Code for button cannot be used for other procedures

Posted on 2013-05-31
5
205 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That helped a great deal.  I was able to change.
0
 
LVL 14

Expert Comment

by:Faustulus
Comment Utility
Great!
Thank you.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

762 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

6 Experts available now in Live!

Get 1:1 Help Now