Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Worksheet wide vars

I wish to have fixed content vars for use in my code in order to avoid using
hardcoded content which would require a lot of editing in case it is
necessary to change a value.

PUBLIC var AS STRING in THISWORKBOOK should do the job.

The question is where can I initialize this var to my desired content and
this initialization be done at workbook startup. How can I define a procedure
that is executed as soon as the workbook is loaded, so that inside it I can
perform any initializations are necessary? Is there a default such procedure
(whose name and existence I obviously ignore...)?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Worksheet wide vars

Depending on how often the values change, you may want to use public
constants instead; e.g.

Public Const MyName = "Ken"
Public Const FavoriteColor = vbRed
Public Const Age = 42
Public Const Weight = "None of your business!"

You can just put all of these in a standard module, maybe name it CONSTANTS,
and edit the values when needed.

"DoctorG" wrote:

I wish to have fixed content vars for use in my code in order to avoid using
hardcoded content which would require a lot of editing in case it is
necessary to change a value.

PUBLIC var AS STRING in THISWORKBOOK should do the job.

The question is where can I initialize this var to my desired content and
this initialization be done at workbook startup. How can I define a procedure
that is executed as soon as the workbook is loaded, so that inside it I can
perform any initializations are necessary? Is there a default such procedure
(whose name and existence I obviously ignore...)?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Worksheet wide vars

....and if you need to perform any functions whenever the workbook is open
there is a default function in ThisWorkbook called Workbook_Open.

Select the ThisWorkbook module in VBAProject and click the pull-down that
has "(General)" in it. Select the Workbook function and the Workbook_Open
sub will appear. Whatever functionality you code into this sub will be
executed whenever the workbook is opened.

"K Dales" wrote:

Depending on how often the values change, you may want to use public
constants instead; e.g.

Public Const MyName = "Ken"
Public Const FavoriteColor = vbRed
Public Const Age = 42
Public Const Weight = "None of your business!"

You can just put all of these in a standard module, maybe name it CONSTANTS,
and edit the values when needed.

"DoctorG" wrote:

I wish to have fixed content vars for use in my code in order to avoid using
hardcoded content which would require a lot of editing in case it is
necessary to change a value.

PUBLIC var AS STRING in THISWORKBOOK should do the job.

The question is where can I initialize this var to my desired content and
this initialization be done at workbook startup. How can I define a procedure
that is executed as soon as the workbook is loaded, so that inside it I can
perform any initializations are necessary? Is there a default such procedure
(whose name and existence I obviously ignore...)?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 93
Default Worksheet wide vars

You can use a sub called auto_open that is fired each time workbook is
opened or a workbook_open event in thisworkbook module to start a code
each time workbook is opened. But you don't need doing this to declare
Publics.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Worksheet wide vars

You both know by now that I am new to Excel programming. Still Charlie's
answer was what I was really asking for since I cannot implement K answer.
Thanks Charlie.

Undoubtedly K's answer was more to the point considering the usage of what I
asked. Unfortunately I cannot do what you suggest K because I don't know how
to create a standard module and place my variables inside. So far I've only
written code to the Microsoft Excel Objects part of the project. I can see
the Modules part but I don't know what it's for. I had discovered the Public
Const statement but its description suggested a standard module and clicking
on it wasn't really helpful to me. If it's not boring for you, pls explain
what a standard module is and how to create one. If I knew that, I wouldn't
need to use the Workbook_Open procedure in this case.

"Charlie" wrote:

...and if you need to perform any functions whenever the workbook is open
there is a default function in ThisWorkbook called Workbook_Open.

Select the ThisWorkbook module in VBAProject and click the pull-down that
has "(General)" in it. Select the Workbook function and the Workbook_Open
sub will appear. Whatever functionality you code into this sub will be
executed whenever the workbook is opened.

"K Dales" wrote:

Depending on how often the values change, you may want to use public
constants instead; e.g.

Public Const MyName = "Ken"
Public Const FavoriteColor = vbRed
Public Const Age = 42
Public Const Weight = "None of your business!"

You can just put all of these in a standard module, maybe name it CONSTANTS,
and edit the values when needed.

"DoctorG" wrote:

I wish to have fixed content vars for use in my code in order to avoid using
hardcoded content which would require a lot of editing in case it is
necessary to change a value.

PUBLIC var AS STRING in THISWORKBOOK should do the job.

The question is where can I initialize this var to my desired content and
this initialization be done at workbook startup. How can I define a procedure
that is executed as soon as the workbook is loaded, so that inside it I can
perform any initializations are necessary? Is there a default such procedure
(whose name and existence I obviously ignore...)?



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Worksheet wide vars

I agree. Overkill for a mere declaration.

I believe I may have succeeded in doing what K Dales suggested. I created a
new module under Modules, which got the name Module2 ( I cannot rename it to
Constants ), and in it I declared my variable with PUBLIC CONST without
putting anything else in that module. My project is working fine. Is this
what a standard module is ???? just declarations and no coding (procedures,
subs and so on)??

Please reply

"Roman" wrote:

You can use a sub called auto_open that is fired each time workbook is
opened or a workbook_open event in thisworkbook module to start a code
each time workbook is opened. But you don't need doing this to declare
Publics.


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Worksheet wide vars

Right click on the VBAProject name in the Explorer and do Insert--Module.
Then double-click the module name (Module1) in the Modules folder. Now you
can create subs and functions for global use in any sheet of the workbook.
At the top of the module is where you can declare public constants, for
example, I prefer to use 1-based arrays and prefer the use of "Yes" and "No"
to "True" and "False", so I always put in these lines at the top of Module1

Option Explicit
Option Base 1

Global Const Yes As Boolean = True
Global Const No As Boolean = False

(etc.)

Then you can start inserting your own functions:

Public Function Whatever(iRow As Long, iCol As Long) As String

....

Later you can right-click on the module name and export it to a folder and
later import it into another workbook. (Wish that part was automatic, like
VB projects.)



"DoctorG" wrote:

(snip)
pls explain
what a standard module is and how to create one. If I knew that, I wouldn't
need to use the Workbook_Open procedure in this case.


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Worksheet wide vars

Hi Doctor G.

You might find it helpful to visit David McRitchie's site an see his
'Getting Started with Macros and User Defined Functions' notes at:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

As a new programmer, you will find a wealth of other matrial on David's
site.

---
Regards,
Norman



"DoctorG" wrote in message
...
You both know by now that I am new to Excel programming. Still Charlie's
answer was what I was really asking for since I cannot implement K answer.
Thanks Charlie.

Undoubtedly K's answer was more to the point considering the usage of what
I
asked. Unfortunately I cannot do what you suggest K because I don't know
how
to create a standard module and place my variables inside. So far I've
only
written code to the Microsoft Excel Objects part of the project. I can see
the Modules part but I don't know what it's for. I had discovered the
Public
Const statement but its description suggested a standard module and
clicking
on it wasn't really helpful to me. If it's not boring for you, pls explain
what a standard module is and how to create one. If I knew that, I
wouldn't
need to use the Workbook_Open procedure in this case.

"Charlie" wrote:

...and if you need to perform any functions whenever the workbook is open
there is a default function in ThisWorkbook called Workbook_Open.

Select the ThisWorkbook module in VBAProject and click the pull-down that
has "(General)" in it. Select the Workbook function and the
Workbook_Open
sub will appear. Whatever functionality you code into this sub will be
executed whenever the workbook is opened.

"K Dales" wrote:

Depending on how often the values change, you may want to use public
constants instead; e.g.

Public Const MyName = "Ken"
Public Const FavoriteColor = vbRed
Public Const Age = 42
Public Const Weight = "None of your business!"

You can just put all of these in a standard module, maybe name it
CONSTANTS,
and edit the values when needed.

"DoctorG" wrote:

I wish to have fixed content vars for use in my code in order to
avoid using
hardcoded content which would require a lot of editing in case it is
necessary to change a value.

PUBLIC var AS STRING in THISWORKBOOK should do the job.

The question is where can I initialize this var to my desired content
and
this initialization be done at workbook startup. How can I define a
procedure
that is executed as soon as the workbook is loaded, so that inside it
I can
perform any initializations are necessary? Is there a default such
procedure
(whose name and existence I obviously ignore...)?



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Worksheet wide vars

In the VBE, do Insert=Module in the menu.

--
Regards,
Tom Ogilvy

"DoctorG" wrote in message
...
You both know by now that I am new to Excel programming. Still Charlie's
answer was what I was really asking for since I cannot implement K answer.
Thanks Charlie.

Undoubtedly K's answer was more to the point considering the usage of what

I
asked. Unfortunately I cannot do what you suggest K because I don't know

how
to create a standard module and place my variables inside. So far I've

only
written code to the Microsoft Excel Objects part of the project. I can see
the Modules part but I don't know what it's for. I had discovered the

Public
Const statement but its description suggested a standard module and

clicking
on it wasn't really helpful to me. If it's not boring for you, pls explain
what a standard module is and how to create one. If I knew that, I

wouldn't
need to use the Workbook_Open procedure in this case.

"Charlie" wrote:

...and if you need to perform any functions whenever the workbook is

open
there is a default function in ThisWorkbook called Workbook_Open.

Select the ThisWorkbook module in VBAProject and click the pull-down

that
has "(General)" in it. Select the Workbook function and the

Workbook_Open
sub will appear. Whatever functionality you code into this sub will be
executed whenever the workbook is opened.

"K Dales" wrote:

Depending on how often the values change, you may want to use public
constants instead; e.g.

Public Const MyName = "Ken"
Public Const FavoriteColor = vbRed
Public Const Age = 42
Public Const Weight = "None of your business!"

You can just put all of these in a standard module, maybe name it

CONSTANTS,
and edit the values when needed.

"DoctorG" wrote:

I wish to have fixed content vars for use in my code in order to

avoid using
hardcoded content which would require a lot of editing in case it is
necessary to change a value.

PUBLIC var AS STRING in THISWORKBOOK should do the job.

The question is where can I initialize this var to my desired

content and
this initialization be done at workbook startup. How can I define a

procedure
that is executed as soon as the workbook is loaded, so that inside

it I can
perform any initializations are necessary? Is there a default such

procedure
(whose name and existence I obviously ignore...)?



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Worksheet wide vars

Thanks to ALL OF YOU for being so prompt and helpful. I really covered some
holes today. I needed to sort out certain "meanings" and link them to what I
already know from my DOS/Clipper programming days.
My project is working and I believe I now understand how to use Modules
effectively. McRitchie's site was also very helpful.
Thanks again - I wish I could do the same for you.


  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Worksheet wide vars

Make the properties window visible in the VBE while the module is selected.
The only property for the module is the change. Change it to Constants by
editing the existing name (module2)

--
Regards,
Tom Ogilvy

"DoctorG" wrote in message
...
I agree. Overkill for a mere declaration.

I believe I may have succeeded in doing what K Dales suggested. I created

a
new module under Modules, which got the name Module2 ( I cannot rename it

to
Constants ), and in it I declared my variable with PUBLIC CONST without
putting anything else in that module. My project is working fine. Is this
what a standard module is ???? just declarations and no coding

(procedures,
subs and so on)??

Please reply

"Roman" wrote:

You can use a sub called auto_open that is fired each time workbook is
opened or a workbook_open event in thisworkbook module to start a code
each time workbook is opened. But you don't need doing this to declare
Publics.




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
Very Wide Spreadsheet Kristine Excel Discussion (Misc queries) 1 December 19th 07 01:32 PM
How wide is a font? Rayo K Excel Discussion (Misc queries) 1 February 2nd 06 07:17 PM
= How to setup vars when a workbookopen event is launched? hcova[_2_] Excel Programming 1 October 21st 04 07:20 PM
Global vars Nath Excel Programming 1 July 13th 04 04:20 PM
Make Worksheet Event available application-wide Bradley C. Hammerstrom Excel Programming 2 May 21st 04 07:31 PM


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