Solved

Using GoSub globally in excel vba?

Posted on 2007-03-23
4
2,155 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:Jeroen Rosink
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

791 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