Solved

Using GoSub globally in excel vba?

Posted on 2007-03-23
4
2,127 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
Comment Utility
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
Comment Utility
So what do I do :)
0
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

744 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

17 Experts available now in Live!

Get 1:1 Help Now