Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing a ribbon with VBA in Excel

Tags:

excel

vba

ribbon

I've already learned that creating a custom tab is in Excel not possible in this! post (unlike i.e. MSProject)

Specifically, can I change the paths of the macros to the current location ?

Edit It seems that this page may lead to the answer, but I still don't know how, though

Some more in-detail description of the situation:

The user will download a file, which contains a list of materials, equipment, labor. Each column contains information about quantities, pricing etc When the user click on this button I want to create (I have created manually), a macro called 'Main' in another workbook launches and copies the whole sheet (the contents) to itself and performs some things that procedures do, on it.

So the problem I'm facing, is twhen I'm sending a new version to the client, he has to put it in the exact location or it won't work. Since there's a mix of Mac and Windows computers involved, I'd rather see a situation where the button is assigned to the procedure when the user opens WorkBook B (the one that contains the code).

This way, a new version has to be openened only once, and then for continuous use, the user can just open the downloaded file, click the appropriate button and WorkBook B will open itself and execute.

Maybe there's other ways to go about this. I haven't checked if it's not easier to assign a button to the quick access toolbar...

like image 739
oneindelijk Avatar asked Aug 29 '13 22:08

oneindelijk


People also ask

How do I create a custom ribbon in Excel VBA?

To add a button to the ribbon, start by right-clicking anywhere on the ribbon or ribbon tabs. Then select Customize the Ribbon. This will open the Excel Options page, and Customize Ribbon should already be highlighted on the left-hand side.

How do I create a custom ribbon in Excel?

To customize the Ribbon, open or create an Excel, Word, or PowerPoint document. Go to the app Preferences and select Ribbon and Toolbar. On the Ribbon tab window, select the commands you want to add or remove from your Ribbon and select the add or remove arrows.

How do I add a VBA macro to a ribbon in Excel?

Add a macro button to the Quick Access ToolbarClick File > Options > Quick Access Toolbar. In the Choose commands from list, click Macros. Select the macro you want to assign a button to. Click Add to move the macro to the list of buttons on the Quick Access Toolbar.


1 Answers

This is some code I use to add a custom toolbar:

Set cbToolbar = Application.CommandBars.Add(csToolbarName, msoBarTop, False, True)

With cbToolbar
    Set ctButton1 = .Controls.Add(Type:=msoControlButton, ID:=2950)
    Set ctButton2 = .Controls.Add(Type:=msoControlButton, ID:=2950)
    Set ctButton3 = .Controls.Add(Type:=msoControlButton, ID:=2950)
End With

With ctButton1
    .Style = msoButtonIconAndCaption
    .Caption = "Set &Picklists"
    .FaceId = 176
    .OnAction = "SetPicklist"
End With

With ctButton2
    .Style = msoButtonIconAndCaption
    .Caption = "Set &Defaults"
    .FaceId = 279
    .OnAction = "SetDefaults"
End With

With ctButton3
    .Style = msoButtonIconAndCaption
    .Caption = "&Visibility Settings"
    .FaceId = 2174
    .OnAction = "VisibilitySettings"
End With


With cbToolbar
    .Visible = True
    .Protection = msoBarNoChangeVisible
End With

The 'OnAction' controls the macro that runs... If you wanted to expand that to run a macro on a specific workbook, use "whatever.xls!MacroName"

like image 80
Simon Avatar answered Oct 05 '22 13:10

Simon