MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 12th, 2002, 04:07 AM   #1
Andrew XJ
Board Regular
 
Join Date: Feb 2002
Location: Singapore
Posts: 77
Default

In VBA, we have commands Workbooks("aBook"), ActiveWorkbook, Workbooks(1), ThisWorkbook. What are the differences between them if Windows("aSheet") is the activeWindow ? Also there are commands WorkSheet("aSheet"), ActiveWorkSheet, Sheets(1),Sheets("aSheet"), What are the differences?
Thx for any explanation.
Andrew XJ is offline   Reply With Quote
Old Mar 12th, 2002, 04:30 AM   #2
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi

Workbooks("aBook.xls")
would refer to ONLY the workbook of that name.


ActiveWorkbook
Refers to the Workboook which is active (the one you see)

Workbooks(1)
Is a Index number being in the order that the open Workbooks were opened.

ThisWorkbook
Always refers to the Workbook that houses the code.


WorkSheet("aSheet")
Refers to a Worksheet by it's sheet tab name. One of the most popular ways but also the worst and unsafest way.

ActiveWorkSheet
As the name suggests

Sheets(1)
Is the Index number going from left to rigt

Sheets("aSheet")
Nearly the same a Worksheets but could also be used for another type of sheet, eg Chart Sheet

The one you haven't mentioned is the most important one and that is
Sheet1
This is a sheets CodeName and is the safest and best way to refer to a sheet (it cannot be changed) Unless deleted ) You can see a Sheets CodeName in the Project Explorer (it's the one NOT in brackets).




_________________
Kind Regards
Dave Hawley
OzGrid Business Applications
Microsoft Excel/VBA Training


[ This Message was edited by: Dave Hawley on 2002-03-12 03:34 ]
Dave Hawley is offline   Reply With Quote
Old Mar 12th, 2002, 04:50 AM   #3
Andrew XJ
Board Regular
 
Join Date: Feb 2002
Location: Singapore
Posts: 77
Default

Thank you, Dave.
Could i ask you two more questions? One is about the scope of the variables in a VBAProject ?
I find that i can't call a function from a UserForm if the function is defined in a standard module even if the function is defined as Public.
How about the variables defined as public? Can you explain in detail? How can i make a function and a variable available for the whole projects?
Andrew XJ is offline   Reply With Quote
Old Mar 12th, 2002, 05:02 AM   #4
Andrew XJ
Board Regular
 
Join Date: Feb 2002
Location: Singapore
Posts: 77
Default

Follow up on the Workbooks(1), ActiveWorkbook:
Dave, could you give some example to show the difference between them? For the last CodeName you mention, i feel very strange since i can often see CodeName such as Sheet1, Sheet11, ..., Sheet1111111111111, but i seldom see Sheet11111111111111 in Office XP.
Andrew XJ is offline   Reply With Quote
Old Mar 12th, 2002, 05:02 AM   #5
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi

To have a variable available throughout your project you must place it at the very TOP of a Standard Module like:

Public iNum as Integer

This would then retain it's value until such time as the Workbook closes, It's changed via VBA or you use the End Statement in your code (End will destroy all variables).

It would be available to both Public modules (Standard) and Private eg UserForm, Workbook, Worksheet etc.

The next level down is the Module level. This means your variable is dimensioned at the top of a Module, but without the Public Statement, eg

Dim iNum as Integer

This means iNum is available to all Procedures within the same Module and also retains it's value much the same as above.


After this is the Procedure level (most common) this is dimmed in the Procedure itself and is only available to the specific Procedure. Once the Procedure has finished the variable value is destroyed.

Hope this help
Dave Hawley is offline   Reply With Quote
Old Mar 12th, 2002, 05:22 AM   #6
Andrew XJ
Board Regular
 
Join Date: Feb 2002
Location: Singapore
Posts: 77
Default

Thx. in fact i defined the variable in Module1. Does it make any difference that i define a public variable at the very top of Module4? Same effect?
About the Auto_open(), i can put it in a standard module and also can put it in ThisWorkbook. Is it? What's the difference externally and internally?
Andrew XJ is offline   Reply With Quote
Old Mar 12th, 2002, 05:27 AM   #7
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi

It makes no diff what the Module is called so long as you use "Public iNum as Integer" placed at the very top.

Auto_Open is old hat! It's only for backward compatability with pre Excel 97. You should use:

Private Sub Workbook_Open()

In the Private Module of the Workbook Object. In fact it will only work here.
Dave Hawley is offline   Reply With Quote
Old Mar 12th, 2002, 06:07 AM   #8
Andrew XJ
Board Regular
 
Join Date: Feb 2002
Location: Singapore
Posts: 77
Default

I used Auto_open() in my projects. Are you sure Workbook_open() can only workbook in private module? I once put Workbook_open() in Module1, which is not a private module? What do you mean that? I want to put Workbook_open() in ThisWorkbook. Is it ok?
Thx
Andrew XJ is offline   Reply With Quote
Old Mar 12th, 2002, 07:08 AM   #9
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi

"Workbook_Open" could no doubt be used a 'standard' procedure name in a 'standard' module but it wouldn't run automatically whenever the Workbook opened. It would have to be in the Private module of the Workbook itself to do that.


Dave Hawley is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 03:25 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes