Creating Ribbon Add-In

masouder

Board Regular
Joined
Jul 5, 2013
Messages
111
Office Version
  1. 2013
Platform
  1. Windows
I am looking for an online source that explains how to build an .xlam add-in that creates a custom ribbon with various commands using VBA. The add-in has to work with 2016 and 365. I thought this would be relatively easy to find, but most of the sites I found on Google include download tools that purport to do most of the work. I would prefer just to learn how to do it on my own. I assume that it is not too difficult to do, but perhaps that is incorrect given the many tools that I found.

Any suggestions are appreciated.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You are indeed incorrect, unless all you want to do is add some controls to the built-in Addins tab, which can be done using the old Commandbars code.
 
Upvote 0
Thanks, Rory. Adding commands to the Addins tab is perfectly fine. Any additional suggestions on where to get more details on how to do this? Or do I just need to search for Commandbars?
 
Upvote 0
Searching for commandbars should find you numerous examples. :)
 
Upvote 0
Searching for commandbars should find you numerous examples. :)

Rory, I'm hoping you might be able to offer a bit more help. I found some sample code which was pretty straightforward. I created a procedure in a file (AddInBuild.xlsm) that added a single item (a button caption) to the Add-In ribbon. Code:

Code:
Sub AddNewCB()
Dim myCommandBar As CommandBar, myCommandBarCtl As CommandBarControl
On Error GoTo AddNewCB_Err
Set myCommandBar = CommandBars.Add(Name:="Sample Toolbar", Position:=msoBarFloating)
myCommandBar.Visible = True
Set myCommandBarCtl = myCommandBar.Controls.Add(Type:=msoControlButton)
With myCommandBarCtl
.Caption = "Center Across"
.Style = msoButtonCaption
.TooltipText = "Display Message Box"
.OnAction = "TestSub()"
   End With
AddNewCB_Err:
Debug.Print Err.Number & vbCr & Err.Description
Exit Sub
End Sub

That item calls a procedure in an Add-In that I created and added to Excel. The add-in procedure centers across selection then displays a message stating that the code is done. Code:
Code:
Public Sub TestSub()
With Selection
.HorizontalAlignment = xlCenterAcrossSelection
    End With
    MsgBox "Done"

End Sub

I ran the procedure to add the item to the Add-Ins ribbon, and that worked fine. I added the Add-In and it is present. The problem is that when I click the new item on the ribbon, the code does not center across selection, but displays the "Done" message so I know the procedure was called. Before clicking the item, I entered text in a cell then selected that cell and two cells to the right.

I have attached the two files that I used.

Any ideas why the center across selection code fails to run?
 
Last edited by a moderator:
Upvote 0
I'd suspect that the two cells to the right weren't actually empty. If you saw the message then the code must have run. However, the OnAction command should not have brackets on the end, it should just be:

Code:
.OnAction = "TestSub"
 
Upvote 0
Rory, amazingly, removing the brackets solved the problem. (I had previously confirmed that the cells to the right were empty.) I'm surprised because with the brackets the code does run - as evidenced by the message at the end - but the code to center across selection did not work. Very odd!
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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
Back
Top