[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Code for button cannot be used for other procedures

Posted on 2013-05-31
5
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

656 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