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 Apr 19th, 2002, 03:52 PM   #1
robfo0
Board Regular
 
Join Date: Feb 2002
Location: Stockton, California
Posts: 281
Default

Hi everyone,

Ive tried doing searches and looking in help, but i cant seem to get the job done.

I would like to define a variable:

currbook=activeworkbook.name

so that ALL modules in the book can use this variable.

I have tried using:
pulic currbook as string
dim currbook as string

then defining currbook in the workbook_open. Is this not the correct way to do this? Help!

Thanks
robfo0 is offline   Reply With Quote
Old Apr 19th, 2002, 04:19 PM   #2
daleyman
Board Regular
 
Join Date: Mar 2002
Location: London, UK
Posts: 167
Default

assuming you pasted code is a typo (ie. should read PUBLIC not PULIC), then i think i know your problem...

I have had this kind of topic before, you need to put the PUBLIC declaration in a new module, that is, not a worksheet or "ThisWorkbook" modules. if you right-click one of these objects and do ADD>module then put the PUBLIC declaration in there.

Don't ask me why publics don't work in worksheet or workbook modules, they just don't seem to.
__________________

<table style="background-color:#0e54be" cellspacing="1" cellpadding="2"><td style="background-color:#ceffff;font-family:arial;color:#072c63;font-size:8pt;">***DALEY** :P**</td></table>
daleyman is offline   Reply With Quote
Old Apr 19th, 2002, 04:29 PM   #3
robfo0
Board Regular
 
Join Date: Feb 2002
Location: Stockton, California
Posts: 281
Default

that was it, thanks
robfo0 is offline   Reply With Quote
Old Apr 19th, 2002, 04:36 PM   #4
robfo0
Board Regular
 
Join Date: Feb 2002
Location: Stockton, California
Posts: 281
Default

ok, another related question. Is there a way that i can use this variable in modules from different workbooks? I tried putting a public in there too, but that didnt work.
robfo0 is offline   Reply With Quote
Old Apr 19th, 2002, 04:40 PM   #5
daleyman
Board Regular
 
Join Date: Mar 2002
Location: London, UK
Posts: 167
Default

no, i'm sure there is a real techy way of doing it, but PUBLIC will still only keep the scope of the variable within the modules collection of the workbook within which it is declared...

I'm surprised you need such a solution, can you not keep all your modules in the same place for this group of operations?
__________________

<table style="background-color:#0e54be" cellspacing="1" cellpadding="2"><td style="background-color:#ceffff;font-family:arial;color:#072c63;font-size:8pt;">***DALEY** :P**</td></table>
daleyman is offline   Reply With Quote
Old Apr 19th, 2002, 04:45 PM   #6
robfo0
Board Regular
 
Join Date: Feb 2002
Location: Stockton, California
Posts: 281
Default

actually, no, the reason i was doing this is because:

I have another workbook, when this workbook opens, i want it to test if another file is open, and if so, it will copy information from that book to the current one. The only problem is, if there is a workbook open, it will always have a different name (they are customer files all formatted the same), so i cant test for name. Also, i have currbook defined in each customer file when it is opened, so to copy the information to the new file, i was hoping to use workbooks(currbook) etc...

Can you think of another way maybe to do this? thanks


edit: i cant open the worksheet from the customer file becuase theres a userform involved which stops the code

[ This Message was edited by: robfo0 on 2002-04-19 15:47 ]
robfo0 is offline   Reply With Quote
Old Apr 19th, 2002, 04:52 PM   #7
daleyman
Board Regular
 
Join Date: Mar 2002
Location: London, UK
Posts: 167
Default

there must be a way, if when the workbook opens you get it to loop through the names of the workbooks that are open, comparing them to the current workbook name, you can then copy information to the existing books?


__________________

<table style="background-color:#0e54be" cellspacing="1" cellpadding="2"><td style="background-color:#ceffff;font-family:arial;color:#072c63;font-size:8pt;">***DALEY** :P**</td></table>
daleyman is offline   Reply With Quote
Old Apr 19th, 2002, 04:58 PM   #8
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Been reading this post. To go back to the Public variable thingy.

The reason that Public variables can't be seen globally from Worksheet and Workbook modules is because these are Private modules. I believe you can mark standard modules as Public or Private, but standard modules are Public by default.

Once I've my dinner I'll maybe look into the variables being global for all open workbooks. (if it's still necessary)

HTH

[ This Message was edited by: Mark O'Brien on 2002-04-19 15:59 ]
Mark O'Brien is offline   Reply With Quote
Old Apr 19th, 2002, 05:04 PM   #9
robfo0
Board Regular
 
Join Date: Feb 2002
Location: Stockton, California
Posts: 281
Default

YES mark please, i would GREATLY appreciate help on this problem, ive been trying to find a way to get this problem done for weeks now, heh

Thanks for the explanation
robfo0 is offline   Reply With Quote
Old Apr 19th, 2002, 08:38 PM   #10
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

daleyman was correct when he talked about looping through the workbooks. I'm going to put together two different examples. The first one uses a global variable, the second one is my preferred method. The reason I dislike global variables is that they take up unnecessary space and they are notoriously difficult to control since anything can change them. For both examples I am assuming that only 2 workbooks are going to be open. 1 workbook is the workbook with the VBA in it, the other workbook is the target workbook with the data that you're going to copy. Since these are just examples, I'll tell you exactly what to do and hopefully you can adapt this to suit your needs. Here we go.

1. Create 2 new workbooks, by default mine were called "Book1.xls" and "Book2.xls", it doesn't matter what they're caled though.
2. In the second book, place this code in "ThisWorkbook":


Private Sub Workbook_Open()
FindWorkbook
End Sub


3. Insert a standard module. (mine is called module1)
4. Put this code in module1:


Public oWorkbook As Workbook 'Global variable - bleh


Public Sub FindWorkbook()

If Workbooks.Count = 2 Then 'If there are only two workbooks open
For Each oWorkbook In Workbooks 'Cycle through the workbooks collection
If Not oWorkbook.Name = ThisWorkbook.Name Then 'If the name of the workbook does not equal thisworkbook, then we have found the other book
Exit For
End If
Next
Else
MsgBox "There is only one work book open.", vbInformation, "Workbook Information"
End If

DisplayWorkbookName 'Call sub routine in the other module

End Sub


5. Create a second module and put this code in it:

Public Sub DisplayWorkbookName()
MsgBox oWorkbook.Name
End Sub


5. Save the book with the VBA and close it down. Keep the other book open.

6. Open the book we've just closed. The message box will display the name of the other open workbook. (oWorkbook is now your Global variable that you requested)

Now, my next post will be very similar, but instead of declaring a global variable, we will be passing the variable from subroutine to subroutine.
Mark O'Brien 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 10:53 AM.


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