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 Feb 20th, 2002, 11:10 AM   #1
Guest
 
Posts: n/a
Default

how do I specify an excel macro that takes a parameter? I need to attach it to a custom menu option.
  Reply With Quote
Old Feb 20th, 2002, 11:34 AM   #2
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

You have several options:

1. if you need to assign a different value to the argument each time you call it (from the menu) you will need to write a short interface macro that prompts you for the desired input, then calls the other macro passing it the value you have entered.

2. if you always call it from the menu with the same value assigned to its input argument, you can make it an optional argument (using the Optional keyword), and even assign it a default value right in the argument list. Example:

Sub ZipState( Optional ZipCode As String = "80120" ) As String

so that if you call ZipState without providing a value for ZipCode, it will automatically enter a value of "80120".

3. If the macro is in a DLL or add-in, you will have to go with option 1 since you can't modify the code to make the argument optional.

__________________
Keep Excelling.

Damon

VBAexpert Excel Consulting
(My other life: http://damonostrander.com )
Damon Ostrander is offline   Reply With Quote
Old Feb 20th, 2002, 11:49 AM   #3
Guest
 
Posts: n/a
Default

thanks Damon

yes I need to keep passing it different values..I am using the onAction command within my VBA code..the things is I cannot have it prompt me everytime I click the menu option, its supposed to know what to do and with what value

thanks
  Reply With Quote
Old Feb 20th, 2002, 11:49 AM   #4
Guest
 
Posts: n/a
Default

thanks Damon

yes I need to keep passing it different values..I am using the onAction command within my VBA code..the things is I cannot have it prompt me everytime I click the menu option, its supposed to know what to do and with what value

thanks
  Reply With Quote
Old Feb 21st, 2002, 10:54 AM   #5
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

Hi again,

Unfortunately the Telepathy option that Microsoft has been working on for so long is still not available in Excel, so there is no way for the routine to read you mind to find out what value you intend for it to use each time you click on the menu item. Would you like it to obtain a value from a worksheet cell, a file, or some other place (perhaps a random number generator?

If you want it to read the value from a cell, simply set the value of the parameter inside the routine. You can use the IsMissing function to determine whether the argument was provided or not, and if not to get it from the worksheet. For example, inside your procedure:

If IsMissing(ZipCode) Then
ZipCode = Worksheets("Codes").[B4]
End If

which gets the zip code from cell B4 on worksheet "Codes" if it is called without any zipcode being provided as an argument.

__________________
Keep Excelling.

Damon

VBAexpert Excel Consulting
(My other life: http://damonostrander.com )
Damon Ostrander is offline   Reply With Quote
Old Aug 3rd, 2004, 04:15 PM   #6
moshea70
New Member
 
Join Date: Apr 2004
Posts: 19
Default Re: Excel Macro with parameter

Dredging up an old post here...

I have the same question as in the original post, which was never quite answered.

I have the following code where I am adding items to a toolbar submenu:

Set mi = m.Controls.Add(msoControlButton, , , , True)
With mi
.Caption = "Activity 1: " & Range("Activity1Desc")
.OnAction = "NavigateToActivity"
.FaceId = 734
.Style = msoButtonIconAndCaption
' .Style = msoButtonCaption ' caption only, no icon, .FaceId not necessary
End With


The problem is that I want to send a parameter to the NavigateToActivity macro. The parameter would be an integer like 1,2,3, etc. But when I change the OnAction line to read something like .OnAction = "NavigateToActivity(1)" I get an error that the macro is not found. When I take out the (1) I get an error that the parameter is required. What is the syntax for passing a paramter in this case???

Thanks!
moshea70 is offline   Reply With Quote
Old Aug 4th, 2004, 11:42 AM   #7
moshea70
New Member
 
Join Date: Apr 2004
Posts: 19
Default Re: Excel Macro with parameter

FYI - I've done some research and haven't found any way to do this without using a workaround like calling a macro that then gets the parameter from somewhere else, like from a cell on the worksheet, or setting up a bunch of macros (in my case, one for each activity) rather than one macro with a parameter to specify the activity.

Bummer.
moshea70 is offline   Reply With Quote
Old Aug 4th, 2004, 11:53 AM   #8
Norie
Board Regular
 
Norie's Avatar
 
Join Date: Apr 2004
Location: Yaren
Posts: 58,375
Default

Why not add a combobox to the toolbar as well?

It could contain all possible values for parameter.

The user chooses which one they want from the combobox and then clicks an Activity button which runs the NavigateToActivity macro.

Change the code of NavigateToActivity to look at the value of the combobox.
__________________
If posting code please use code tags.
Norie is offline   Reply With Quote
Old Aug 4th, 2004, 04:10 PM   #9
moshea70
New Member
 
Join Date: Apr 2004
Posts: 19
Default Re: Excel Macro with parameter

Still thinking about the combo box idea, but just came accross this:

http://www.exceltip.com/at-424|Menus%2C_Toolbars%2C_Status_bar_in_VBA-Pass_arguments_to_macros_from_buttons_and_menus_using_VBA_in_Microsoft_Excel

Which give the key line of code to pass a parameter (in this case, a caption name) as:

.OnAction = "'MyMacroName2 """ & .Caption & """'"

Just what I need! Only, I can't get it to work.

This is my code... any ideas? When I run it I don't get an error, but the macro doesn't run either.


Sub AddSupportSheetsSubMenu(mm As CommandBarPopup, blnBeginGroup As Boolean)
Dim sh As Worksheet
Dim m As CommandBarPopup, mi As CommandBarButton
If mm Is Nothing Then Exit Sub
' create the submenu
Set m = mm.Controls.Add(msoControlPopup, , , , True)
With m
.BeginGroup = blnBeginGroup
.Caption = "Support and scratch sheets..."
End With

For Each sh In Worksheets
' add a menu item
Set mi = m.Controls.Add(msoControlButton, , , , True)
With mi
.Caption = sh.Name
.OnAction = "'MyMacroName2 """ & .Caption & """'"
.FaceId = 66
.Style = msoButtonIconAndCaption
End With
End If
Next sh
Set mi = Nothing
Set m = Nothing
End Sub

....
Sub MyMacroName2(SheetName as String)
Sheets(SheetName).Select
End Sub
moshea70 is offline   Reply With Quote
Old Aug 4th, 2004, 04:24 PM   #10
Norie
Board Regular
 
Norie's Avatar
 
Join Date: Apr 2004
Location: Yaren
Posts: 58,375
Default

What is it you are trying to acheive?

Is it some sort of menu so you can jump from sheet to sheet?

If so I have code (at work) that on opening creates a combobox containing all the sheets in the workbook.

It also creates a 'Go' button, that when the user presses it Excel jumps to the sheet chosen in the combobox.

Is that what you're trying?
__________________
If posting code please use code tags.
Norie 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:33 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