Solved

Using GoSub globally in excel vba?

Posted on 2007-03-23
4
2,131 Views
Last Modified: 2008-01-09
I actively use GoSub to call up a section of code that is repeated. I was wondering if I could use the GoSub globally, the same way that a variable can be used within all modules? Anyone know how to do that?
0
Comment
Question by:tiehaze
  • 2
4 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 18782884
No, but you can write a sub procedure that can be called from anywhere.  The main disadvantage (or advantage, depending on your mindset) is that the subroutine has the same scope as the calling statement and therefore, access to all the callers variables.  The sub-routine, on the other hand, has it's own scope and cannot access variables that are private to the caller unless they are passed by reference as arguments.
0
 

Author Comment

by:tiehaze
ID: 18782893
So what do I do :)
0
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 18783038
For example, add to the top of your module:

Public Sub PopupMessage (YourMessage as string)
'this is a trivial subroutine to popup a message. Replace with your re-usable code and a more suitable name.
   msgbox (YourMessage)
End Sub

Then from somewhere else:
Call PopupMessage ("Hello World")
0
 
LVL 33

Expert Comment

by:roos01
ID: 18783351
What you could do is creating an Add-in which contains your macro.
You could call this macro to use the lines:

Call YourMacro()

or to pass a variable value with it you could use it like:
Call YourMacro(YourValue)

for some info about creating an add-in:
http://www.exceltip.com/show_tip/Cells,_Ranges,_Rows,_and_Columns_in_VBA/Create_an_addin_in_Excel_97,_2000,_2002_using_VBA_in_Microsoft_Excel/495.html

I hope this works for you
regards,
Jeroen
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

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

930 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

14 Experts available now in Live!

Get 1:1 Help Now