Substitute command button for button on the ribbon

dim4x4

Board Regular
Joined
May 8, 2002
Messages
108
Hello!

I have a command button with the following code:

VBA Code:
Private Sub CommandButton1_Click()

    If vTurnOn = False Then
       vTurnOn = True
    Else
       vTurnOn = False
    End If

End Sub

How do I make a button on the ribbon that does the same thing that the code?

Thank you!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
First find some secure location and download "Custom UI Editor For Microsoft Office". It allows you to create custom ribbon.
Open UI Editor and go to file open and open your Excel workbook.
Click "Insert" and select version of "Office". On the right side of UI Editor paste this code...
Excel Formula:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <ribbon>
        <tabs>
            <tab id="customTab2" label="My Tools">
                <group id="customGroup" label="My Button">
                    <button id="customButton"
                        label = "Turn On Button"
                        size = "large"
                        onAction="TurnOn" />
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>
In the UI Editor in the menu click "Validate" icon to check possible errors.
Folow validate instructions. When you recive "Custom UI is well formed" save UI Editor and close.
Now open your workbook and create standard module.
Paste this code in this module...
VBA Code:
Dim vTurnOn As Boolean

Sub TurnOn(control As IRibbonControl)

    If vTurnOn = False Then
       vTurnOn = True
       MsgBox "OK"
    Else
       vTurnOn = False
    End If
    
End Sub
Save workbook and reopen. You will see new tab with new button.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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