LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default How to Set a Variable in Another Module?

Basic Question: How do I assign a value to a variable in a code module from
the ThisWorkbook module, Workbook_Open event?

Background Context: I've got a worksheet (called "Assumptions") in a
workbook that needs to have Calculation set to manual. I want to be polite
to the user and store the existing Application.Calculation state so I can
restore it when the user switches to another worksheet. In other words, do
not just switch Calcualtion to manual and leave it there.

The following Worksheet-level code works fine AS LONG AS I closed the
workbook in another sheet and then select the Assumptions worksheet to
trigger the Worksheet_Activate event handler. This initializes the
module-leve global variable OrigCalc via the Worksheet_Activate handler.


Option Explicit

Dim OrigCalc As Long
' Need to store original state to a Long data type:
' xlAutomatic = -4105
' xlSemiautomatic = -2
' xlManual = -4135

Private Sub Worksheet_Activate()
OrigCalc = Application.Calculation
Application.Calculation = xlManual
End Sub

Private Sub Worksheet_Deactivate()
Application.Calculation = OrigCalc
End Sub

If, however, I closed the workbook with worksheet "Assumptions" activated,
the next time I open the workbook, the global "OrigCalc" does not get
initialized (it appears that the Worksheet_Activate event is not triggered
after the Workbook_Open event). In this case, the immediate window shows...
?origcalc
#NOTHING#

So, I thought a simple solution would be...

Private Sub Worksheet_Deactivate()
If Not(OrigCalc = Nothing) then
Application.Calculation = OrigCalc
End If
End Sub

But this doesn't work. I get a "Invalid Use of Object" error with "Nothing"
highlighted.

One solution would be to have the Workbook_Open event handler initialize the
OrigCalc global in my main module.

So, my question to you is how do I assign a value to a variable in a code
module from the ThisWorkbook module, Workbook_Open event?

Sorry for the long explanation for such a simple problem.

Steve

 
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
STATIC VARIABLE NOT AVAILABLE IN ANOTHER MODULE CAPTGNVR Excel Discussion (Misc queries) 2 February 1st 07 07:30 PM
Variable in more than one module Nick M[_3_] Excel Programming 2 November 8th 05 07:20 PM
Assign Value to Module Level Variable monir Excel Programming 21 March 23rd 05 03:05 AM
Variable from a sheet module in a class module in XL XP hglamy[_2_] Excel Programming 2 October 14th 03 05:48 PM
module-level variable lifetime Jessie[_2_] Excel Programming 0 July 30th 03 09:31 PM


All times are GMT +1. The time now is 08:04 AM.

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

About Us

"It's about Microsoft Excel"