Link to home
Start Free TrialLog in
Avatar of leezac
leezac

asked on

Code for button cannot be used for other procedures

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.
Avatar of rspahitz
rspahitz
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of Faustulus
Faustulus
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of leezac
leezac

ASKER

That helped a great deal.  I was able to change.
Great!
Thank you.