Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ed Ed is offline
external usenet poster
 
Posts: 399
Default Set module-wide objects?

Is it possible to set a worksheet object the will be available to every
macro in a single module, versus having to re-set the same object in each
macro? I can declare the variable across the entire module, but the objects
seem to vanish ate each individual End Sub.

Ed


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Set module-wide objects?

The first sub you run must set the variable to the worksheet. The code must
be in a general module, not in a worksheet or the thisworkbook module. At
the top of the module:

Public mySheet as Worksheet

Sub Macro1()
set mySheet = Worksheets(1)
End Sub

Sub Macro2()
if mysheet is nothing then macro1
msgbox mysheet.Name
End Sub

Sub Macro3()
if mysheet is nothing then macro1
msgbox mysheet.Range("A1").Value
End Sub

--
Regards,
Tom Ogilvy



"Ed" wrote in message
...
Is it possible to set a worksheet object the will be available to every
macro in a single module, versus having to re-set the same object in each
macro? I can declare the variable across the entire module, but the

objects
seem to vanish ate each individual End Sub.

Ed




  #3   Report Post  
Posted to microsoft.public.excel.programming
Ed Ed is offline
external usenet poster
 
Posts: 399
Default Set module-wide objects?

Tom: Would I be able to fire this from a Workbook_Open macro in
ThisWorkbook? Or would that destroy the object when it left that module?

Ed

"Tom Ogilvy" wrote in message
...
The first sub you run must set the variable to the worksheet. The code

must
be in a general module, not in a worksheet or the thisworkbook module. At
the top of the module:

Public mySheet as Worksheet

Sub Macro1()
set mySheet = Worksheets(1)
End Sub

Sub Macro2()
if mysheet is nothing then macro1
msgbox mysheet.Name
End Sub

Sub Macro3()
if mysheet is nothing then macro1
msgbox mysheet.Range("A1").Value
End Sub

--
Regards,
Tom Ogilvy



"Ed" wrote in message
...
Is it possible to set a worksheet object the will be available to every
macro in a single module, versus having to re-set the same object in

each
macro? I can declare the variable across the entire module, but the

objects
seem to vanish ate each individual End Sub.

Ed






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Set module-wide objects?

You should declare the public variable in a general module. You can set it
in the Workbook_Open Event.

--
Regards,
Tom Ogilvy

"Ed" wrote in message
...
Tom: Would I be able to fire this from a Workbook_Open macro in
ThisWorkbook? Or would that destroy the object when it left that module?

Ed

"Tom Ogilvy" wrote in message
...
The first sub you run must set the variable to the worksheet. The code

must
be in a general module, not in a worksheet or the thisworkbook module.

At
the top of the module:

Public mySheet as Worksheet

Sub Macro1()
set mySheet = Worksheets(1)
End Sub

Sub Macro2()
if mysheet is nothing then macro1
msgbox mysheet.Name
End Sub

Sub Macro3()
if mysheet is nothing then macro1
msgbox mysheet.Range("A1").Value
End Sub

--
Regards,
Tom Ogilvy



"Ed" wrote in message
...
Is it possible to set a worksheet object the will be available to

every
macro in a single module, versus having to re-set the same object in

each
macro? I can declare the variable across the entire module, but the

objects
seem to vanish ate each individual End Sub.

Ed








  #5   Report Post  
Posted to microsoft.public.excel.programming
Ed Ed is offline
external usenet poster
 
Posts: 399
Default Set module-wide objects?

Thanks, Tom. I appreciate the boost.

Ed

"Tom Ogilvy" wrote in message
...
You should declare the public variable in a general module. You can set

it
in the Workbook_Open Event.

--
Regards,
Tom Ogilvy

"Ed" wrote in message
...
Tom: Would I be able to fire this from a Workbook_Open macro in
ThisWorkbook? Or would that destroy the object when it left that

module?

Ed

"Tom Ogilvy" wrote in message
...
The first sub you run must set the variable to the worksheet. The

code
must
be in a general module, not in a worksheet or the thisworkbook module.

At
the top of the module:

Public mySheet as Worksheet

Sub Macro1()
set mySheet = Worksheets(1)
End Sub

Sub Macro2()
if mysheet is nothing then macro1
msgbox mysheet.Name
End Sub

Sub Macro3()
if mysheet is nothing then macro1
msgbox mysheet.Range("A1").Value
End Sub

--
Regards,
Tom Ogilvy



"Ed" wrote in message
...
Is it possible to set a worksheet object the will be available to

every
macro in a single module, versus having to re-set the same object in

each
macro? I can declare the variable across the entire module, but the
objects
seem to vanish ate each individual End Sub.

Ed










Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
CLASS MODULE & SIMPLE MODULE FARAZ QURESHI Excel Discussion (Misc queries) 1 September 7th 07 09:32 AM
code in module A to not execute a Worksheet_SelectionChange sub of another module Jack Sons Excel Discussion (Misc queries) 4 December 11th 05 11:52 PM
Dynamically Assign Objects to Form Objects. The Vision Thing Excel Programming 2 December 11th 04 04:02 PM
Unable to remove Sheet objects in the Microsoft Excel Objects Adrian[_7_] Excel Programming 1 August 26th 04 10:49 PM
Variable from a sheet module in a class module in XL XP hglamy[_2_] Excel Programming 2 October 14th 03 05:48 PM


All times are GMT +1. The time now is 11:25 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"