SCRIPT TO install Macro(VB) to Users EXCEL TOOL BAR

HotNumbers

Well-known Member
Joined
Feb 14, 2005
Messages
728
Does anyone know how to install a Macro Button and assign it to a Button on a users EXCEL toolbar.

I have written code that needs to be installed to number of users tools bars. Users are not excel savy need to have it installed and assigned to button.

PLS HELP
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

GettingThere

New Member
Joined
Mar 10, 2009
Messages
19
Hi,

This isn't the easiest thing to do but here's a few ideas as a start...

1. I tried recording adding a button and came up with:

Code:
    Application.CommandBars("Standard").Controls.Add Type:=msoControlButton, ID:=2950, Before:=9

This will add the smiley face button on the users standard toolbar

2. Installing a macro programatically. The only way I know of doing this is a little longwinded, depending on the size of your macro:

Code:
Sub Add_Macro()
Set VBP = ThisWorkbook.VBProject
Set VBModule = VBP.VBComponents.Item("ThisWorkbook").Codemodule
'All of the code of the macro you want to add goes here...
VBModule.AddFromString ("Sub Added_Macro" & vbCrLf & "msgbox (""Hello World!"") " & vbCrLf & "End Sub")
End Sub

- Note that for VBA to add code to itself the user needs to go to Tools - Macro - Security - Trusted Sources and 'Trust Access to Visual Basic Project'

Combining these two will allow you to send a sheet to all your users - they click a button to start it off and then it will add the button and install the macro. The two points I can't advise on yet are:

1. How to assign the macro to the button, and
2. Whether the 'add macro' macro can be used to add itself to the personal macro folder.

I'm going to do some research on this because I'm actually interested in finding it out too so if I work any more out I'll let you know!! lol

Hope that helps.
A.
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,383
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Your best bet is to distribute the code as an add-in that the users can install. At the simplest level, you just need code like this in the ThisWorkbook module of your add-in file:
Code:
Private Sub Workbook_Open()
    Set cbr = Application.CommandBars("Worksheet Menu Bar")
    Set ctl = cbr.Controls.Add(Type:=msoControlButton, temporary:=True)
    With ctl
        .Caption = "Your caption here"
        .OnAction = "'" & Thisworkbook.name & "'!macro_name_here"
        .Style = msoButtonCaption
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,213
Messages
5,985,300
Members
439,956
Latest member
FrazzledCat

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top