LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 7,247
Default Declare and Set Public variables

You cannot assign a value to a variable in the variable declarations.
Thus, code like

Public WB As Workbook = ActiveWorkbook

will fail because it assigns the value in the declaration. You need to
separate the declaration from the assignment. In a regular code
module, enter the following in the Declarations section (before and
outside of any procedure declaration):

Public DPE As Workbook

Then, you'll have to assign the ActiveWorkbook to the variable. You
can do this anywhere, but if you want to do it when the workbook
opens, use the following in the ThisWorkbook code module:

Private Sub Workbook_Open()
Set DPE = ActiveWorkbook
End Sub

This is rather pointless because the ActiveWorkbook will be the
workbook that contains the code. You can always reference the workbook
that contains the code, regardless of what workbook happens to be
active, with ThisWorkbook. E.g.,

Debug.Print ThisWorkbook.Name

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Wed, 28 Jan 2009 06:01:19 -0800 (PST), jlclyde
wrote:

I am trying to declare and set a public variable. The variable is
Workbook, and I am trying to set the active workbook as a variable to
be used in other macros without having to reset this.

I am thinking something like Public DPE As Workbook = Activeworkbook
or thisworkbook.

Any help will be greatly appreciated,
Jay

 
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
public variables johnny Excel Discussion (Misc queries) 7 February 27th 08 03:44 PM
Public variables johnny Excel Discussion (Misc queries) 2 February 24th 08 05:05 AM
Public Variables Jerry McNabb Excel Discussion (Misc queries) 0 February 24th 08 01:26 AM
how to declare local variables for Excel.Workbook at runtime. Daffo Excel Discussion (Misc queries) 0 October 9th 06 12:19 PM
How to efficiently declare variables Jeff Excel Discussion (Misc queries) 2 June 29th 06 01:56 PM


All times are GMT +1. The time now is 02:39 PM.

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"