Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Question about scoping variables

Excel 2000
Windows 2k Pro

I'll try to be brief:

I have a workbook that when it opens closes all other open workbooks. Not
very flexible, I know, but down the line I'll add the appropriate warnings
and whistles. Anyway, once in this workbook the user may be prompted to
open another workbook so that they can copy and paste data from that
workbook to the original workbook. There are (at the time of this writing)
three standard modules and three userforms involved also, in addition to
some code in the ThisWorkbook module.

What I'm having a hard time with is being able to keep track of which
workbook is which, and it may be that I need to activate or select a
workbook from either a standard module or a userform module. I know how to
set a variable equal to the active workbook, but often times the userform
module won't recognize it or it will work once then not again after that. I
know this must be a variable scope issue, but I'm confused on where the
variables should be declared. I know that Public variables must be declared
in a standard module, but I'm having limited success with that.

For arguments sake the workbook variables are MyWB and TempWB, with MyWB
being the original, and there will never be more than two workbooks open at
a time. So, what is the best way to declare these workbook variables so
that ANY module can access them? Or have I bitten off more than I can chew?

All hints and advice greatly appreciated.

-gk-


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Question about scoping variables

in a standard module (as you say)

Public MyWB as Workbook
Publc TempWB as Workbook

Sub OpenBook()
sStr = "C:\My Documents\MyFile.xls"
set TempWb = Workbooks.Open(sStr)
set MyWB = Thisworkbook ' workbook containing the code
End Sub

You have to set the variables so they have values.

Make sure you don't have any plain END statements in your code. This resets
variables. Don't hit the reset button in the VBE. This clears you
variables as well.

--
Regards,
Tom Ogilvy



TBA wrote in message
...
Excel 2000
Windows 2k Pro

I'll try to be brief:

I have a workbook that when it opens closes all other open workbooks. Not
very flexible, I know, but down the line I'll add the appropriate warnings
and whistles. Anyway, once in this workbook the user may be prompted to
open another workbook so that they can copy and paste data from that
workbook to the original workbook. There are (at the time of this

writing)
three standard modules and three userforms involved also, in addition to
some code in the ThisWorkbook module.

What I'm having a hard time with is being able to keep track of which
workbook is which, and it may be that I need to activate or select a
workbook from either a standard module or a userform module. I know how

to
set a variable equal to the active workbook, but often times the userform
module won't recognize it or it will work once then not again after that.

I
know this must be a variable scope issue, but I'm confused on where the
variables should be declared. I know that Public variables must be

declared
in a standard module, but I'm having limited success with that.

For arguments sake the workbook variables are MyWB and TempWB, with MyWB
being the original, and there will never be more than two workbooks open

at
a time. So, what is the best way to declare these workbook variables so
that ANY module can access them? Or have I bitten off more than I can

chew?

All hints and advice greatly appreciated.

-gk-




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
Not at all clear on use of variables and/or object variables JMay-Rke Excel Discussion (Misc queries) 11 July 4th 08 06:36 PM
Lookup Question with multiple dynamic variables Robin Excel Worksheet Functions 14 June 12th 08 09:40 PM
Lookup Question Based upon 2 Variables Lois Excel Worksheet Functions 2 May 23rd 08 11:37 PM
general question about variables integreat Excel Discussion (Misc queries) 2 May 20th 06 07:29 AM
Using variables in a name Vispy Excel Discussion (Misc queries) 4 February 22nd 06 01:17 AM


All times are GMT +1. The time now is 06:45 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"