Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 21st, 2009, 04:14 PM
Authorized User
 
Join Date: Oct 2006
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing value from menu item into macro

Maybe someone has done this, I cannot figure it out.

I have a custom menu that is created from a table in a worksheet. This is done and works.

The menu is essentially a list, and I want it to run the same code no matter what item is clicked. The difference is that the item name (or the caption if you will) is passed into the sub.

The subname would be "MapPlanting(Crop as string)".

Crop would be one of the 20 potential items, and the sub could take over from there and find the criteria associated with the selection.

I cannot get it to pass in the variable. When the menu is built I tried the following:

PositionOrMacro = "MapPlanting(" & Chr(34) & MenuArray(3, y) & Chr(34) & ")"


But this yields the error message:

"The macro 'MapPlanting("Cab") cannot be found."

Anyone know how to do this??
 
Old May 21st, 2009, 09:26 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

You can try Application.Run. For example:

Code:
 
Sub SubWithArgs(ByVal sMsg)
    MsgBox sMsg
End Sub
 
Sub CallSubWithArgs()
Application.Run "SubWithArgs", "Tom"
End Sub
You can use

Application.Run "MapPlanting" , MenuArray(3, y)
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old May 26th, 2009, 08:41 AM
Authorized User
 
Join Date: Oct 2006
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your response.

I don't understand what you are telling me. What I keep running into is that the selection of an item from the drop down menu results in a command that doesn't allow me to pass arguments.

If I do your SubWithArgs(ByVal sMsg) I get the same result, ie SubWithArgs("Corn") will still give the macro not found message.

What I ended up doing was having the routine that builds the custom menu build all the appropriate subroutines automatically by inserting code into a module. All these subs call another routine (kind of like you suggest) and pass the arguments to it. The real trick here was that I want the menu to change according to the user, thus the need for dynamic on-the-fly subs.

Would still like to know if what you propose can work, but I wasn't able to.

Thanks again





Similar Threads
Thread Thread Starter Forum Replies Last Post
Menu Item Highlights tedr BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 3 May 14th, 2009 12:24 AM
right click on a menu item chuckcottle C++ Programming 3 May 14th, 2007 10:34 AM
cannot add a new item to this menu ... paulmarshall General .NET 0 April 18th, 2007 03:03 PM
how create menubar, menu, menu item in xsl vijayanmsc XSLT 1 June 5th, 2006 06:43 AM
Menu Help in statusbar at mousemove over Menu item Kaustav VB Components 1 September 14th, 2005 09:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.