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 28th, 2002, 05:37 AM   #1
LLoyd
New Member
 
Join Date: Mar 2002
Location: Cambridge, UK
Posts: 4
Default

I need to run a macro, allow the user to do some stuff in Excel, and then run another macro. The thing is, the second macro needs to use variables produced by the first macro and when the first macro ends the variables go out of scope and become empty. The variables are in a standard module.

Is there a way to keep VB variables in scope and allow the user to use VB, or do I have to come up with a way of using the sheets to store variables (which will make my second macro slower)?

Cheers,
LLoyd
LLoyd is offline   Reply With Quote
Old Mar 28th, 2002, 06:55 AM   #2
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi
Declare your variables as Public in the general declarations part of a standard module.
The general declarations section is the very top of the module:
Example

Not
Dim YourVariable
Use this
Public YourVariable

Will keep it's value through the life of the workbook unless you change it.

Have a Nive Day!

Tom
Tom Schreiner is offline   Reply With Quote
Old Mar 28th, 2002, 06:59 AM   #3
LLoyd
New Member
 
Join Date: Mar 2002
Location: Cambridge, UK
Posts: 4
Default

That's what I'm doing.
LLoyd is offline   Reply With Quote
Old Mar 28th, 2002, 07:03 AM   #4
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi LLoyd

Make sure none of the Procedures are using the "End" statement. These will destroy public variables.


Dave Hawley is offline   Reply With Quote
Old Mar 28th, 2002, 07:03 AM   #5
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Those are the rules..
Are you sure you are declaring in a standard module?

Not a sheet or work book?

It should work.

Atleast those are the rules

Tom
Tom Schreiner is offline   Reply With Quote
Old Mar 28th, 2002, 09:53 AM   #6
AJ
Board Regular
 
Join Date: Mar 2002
Location: =ActiveCell.Address
Posts: 478
Default

I have a feeling that Lloyds point has been missed.
Or at least if it hasn't then I have a similar question because the way I understood his post is something I've been wondering myself...

It's all very well using Public variables so that they hold firm accross subroutines and modules, but what if at any given point you want to pause the running of a macro, hand control of the spreadsheet back to the user, and then pick it up again at a later point.

Basially, a permanent way of adding a break point I guess, but in a more user friendly fashion.

Any ideas, most welcome.

Thanks
AJ
AJ is offline   Reply With Quote
Old Mar 28th, 2002, 11:17 AM   #7
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

You could use a simple class module to keep variables alive between handing control back to the user.

For example.

1.Insert a class module to your project, hopefully it will be called "Class1", if not, rename it to be "Class1".

1˝. Insert this code into Class1:


Public vMyValue As Variant



2.Put this code in a standard module:


Dim oClass As Class1

Public Sub main()

If oClass Is Nothing Then
Set oClass = New Class1
oClass.vMyValue = InputBox("Enter a value")
End If

MsgBox oClass.vMyValue

End Sub


3. Stick a commandbutton on a worksheet and set the macro to "main", or make it run "main" on the click event.

4. Click the button to input a value, do it etc. Mess around with Excel. Then click the button again.

Notes:

The object can be destroyed in three ways.

1. Close Excel
2. Set the object to nothing i.e. "Set oClass = Nothing"
3. If you don't handle errors and an error occurs.

HTH

_________________
[b] Mark O'Brien

[ This Message was edited by: Mark O'Brien on 2002-03-28 10:18 ]
Mark O'Brien is offline   Reply With Quote
Old Mar 28th, 2002, 11:25 AM   #8
Juan Pablo González
MrExcel MVP
 
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
Default

Following Mark's code, I created this on a empty module.

Code:
Option Explicit

Public Var As Integer

Sub T()
If Var = 0 Then
    Var = Application.InputBox("Number", "Title", Type:=1)
Else
    MsgBox Var
End If
End Sub
If, when Inputed, you set the number to anything other than 0, the Var variable will mantain its value, even if you "mess around with Excel"

__________________
Regards,

Juan Pablo González
http://www.juanpg.com
Juan Pablo González is offline   Reply With Quote
Old Mar 28th, 2002, 11:29 AM   #9
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Yeah, I just wanted to stick in some class module stuff.

My code is best used if you're using userforms and want to keep the variable live after the userform's been unloaded.
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Mar 28th, 2002, 11:50 AM   #10
Juan Pablo González
MrExcel MVP
 
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
Default

Well... yes. But, I always prefer to put those variables in Regular modules...

Anyway, that's just coding style !
__________________
Regards,

Juan Pablo González
http://www.juanpg.com
Juan Pablo González 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 04:11 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