Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Menu list with Multiple Macros in Userform

Posted on 2011-02-21
9
Medium Priority
?
1,034 Views
Last Modified: 2012-05-11
Hi,  I have a number of macros that would like all to be displayed on a list and let a user pick whichever they need. I guess userform with option buttons is a means to do it. I will greaetly appreciate if somebody could show me how to do it in an example.

Thank you, N.
0
Comment
Question by:NewToVBA
  • 5
  • 2
  • 2
9 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 34946865
Have you thought about creating a Command Bar Menu?

Here are some tips for creating command bars -

http://msdn.microsoft.com/en-us/library/aa141022(v=office.10).aspx
http://www.ozgrid.com/VBA/custom-menus.htm

Dave
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34946952
Is this what you're looking for.  I culled commandbar work from some addin work I was doing.  You could even make this an addin and I'll help you with that if you're headed that direction.

For now, test and see if this is what you want.  After loading, go to Addins (because that's where I put the command bar) and run the "great" macros.

Dave

Here's the code in ThisWorkbook that builds the commandbar, and deletes them on file close:

Private Sub Workbook_Open()
'Commandbar stuff

    Set CommandBarMenu1 = Application.CommandBars(1).Controls.Add(Type:=msoControlPopup, temporary:=True) 'msocontrolpopup
    CommandBarMenu1.Caption = "&Great macros!"
    Set CommandBarControl = CommandBarMenu1.Controls.Add(Type:=msoControlButton, temporary:=True) 'msocontrolbutton
    CommandBarControl.Caption = "&MyMacro_1"
    CommandBarControl.OnAction = "MyMacro_1"
    
    Set CommandBarControl = CommandBarMenu1.Controls.Add(Type:=msoControlButton, temporary:=True) 'msocontrolbutton
    CommandBarControl.Caption = "&MyMacro_2"
    CommandBarControl.OnAction = "MyMacro_2"
    
    Set CommandBarControl = CommandBarMenu1.Controls.Add(Type:=msoControlButton, temporary:=True) 'msocontrolbutton
    CommandBarControl.Caption = "E&xit Great macros!"
    CommandBarControl.OnAction = "MyGreatMacro_Terminate"
    

End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call MyGreatMacro_Terminate
End Sub

Open in new window


And here's the code in MODULE 1:

Enjoy!

Dave
CommandBars-r1.xlsm
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34946980
Erm, I really wouldn't recommend using CommandBarControl as a variable name! ;)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 42

Expert Comment

by:dlmille
ID: 34947036
And here it is as an add-in:
Note there's an option to work in debug mode (just set cDebug = true somewhere before the code runs).  if you run the macro tempUnmakeMacro, it tells Excel its not an addin and turns cDebug on.  So, if you hit the EXIT, the file closes (as opposed to unloading the addin)...  Anyway, perhaps more than you need, but just in case, this is how I do it.

Exiting the Addin, takes it off the Users's INSTALLED list, if they installed via Excel Options.  They can add it back, if they want to ressurrect it, otherwise its always there for them.

Copy the addin to the Addins folder, then you can add it via Excel Options.  Or just double-click on the addin from whereever you save it to load.  My users do it both ways.

 
'in a public module, the following declarations (so you can close the menu bar later...
Public sourceWb As Variant
Public cDebug As Boolean, addInName As String
Public CommandBarMenu1 As CommandBarPopup, CommandBarMenu2 As CommandBarPopup
Public CommandBarControl As CommandBarButton
Public Sub MyGreatMacro_Terminate()
    On Error Resume Next 'if exit commandbar menu, then this fires, then workbook_BeforeClose fires, so would get an error the second time around
    
    CommandBarMenu1.Delete
    
    If cDebug Then
        Application.EnableEvents = False
        ActiveWorkbook.Close
        Application.EnableEvents = True
    Else
        AddIns(addInName).Installed = False
        ThisWorkbook.Close SaveChanges:=False
    End If
    
    On Error GoTo 0
End Sub
Public Sub tempUnmakeAddin()
     ThisWorkbook.IsAddin = False
End Sub

Open in new window


Note, I moved my macros to a MODULE, and left the addin handling in a module named as such.

Feel free to ask questions.  It would be pretty easy to keep adding macros to your menubar from here...

Enjoy!

Dave
CommandBars-r1.xlam
0
 
LVL 42

Accepted Solution

by:
dlmille earned 2000 total points
ID: 34947078
@rorya - good one - better to stay away from the base names of controls.  Attached is the modified addin using CommandBarControl1 as the variable name, though I've never had problems with this - better to be safe!

Dave
CommandBars-r1.xlam
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34947291
Here's a couple more options - putting the macro names and descriptions on the sheet with a button you click to run the macro, and creating a hyperlink that is "followed" to test and run the right macro (see code in the Sheet1 codepage, as below).
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    
    Select Case Target.Name
    
    Case "MyMacro_1":
        Call MyMacro_1
        
    Case "MyMacro_2":
        Call MyMacro_2
        
    Case Else
        'do nothing
        
    End Select

End Sub

Open in new window

See attached,

Dave
MacroSheetOOptions-r1.xlsm
0
 

Author Closing Comment

by:NewToVBA
ID: 34947397
Amaizing! Fanstatic solutions...
0
 

Author Comment

by:NewToVBA
ID: 34947421
Thank you very much Dave and Rorya!  I am very impressed with your skills...
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34947464
All the credit is Dave's - I didn't do a thing! :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question