Solved

Using GoSub globally in excel vba?

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

688 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