Solved

Using GoSub globally in excel vba?

Posted on 2007-03-23
4
2,138 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

774 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