adding macro in ribbon via customUIEditor

Axelle

New Member
Joined
Apr 16, 2018
Messages
19
Dear Mr. Excel,

I made a customized ribbon in excel, where a number of standard buttons have been added for my dashboard.
Now I have written a macro that I also want to add in the customized ribbon.

In CustomUIEditor I added:
HTML:
                <!-- BUTTON: Macro-->
                <group 
                    id="Macro" 
                    label="Save as offer">
                    <button 
                        id="CopySave" 
                        label="CopySave" 
                        imageMso="AcceptInvitation" 
                        size="large" 
                        onAction="ModCopySave" 
                        enabled="true"
                        screentip="Start macro"/>
                </group>

In de module for the ribbon I added:
Code:
'Callback for CopySave onAction
Sub ModCopySave(control As IRibbonControl)
    Dim wb As Workbook

    Set wb = Workbooks.Add
    ThisWorkbook.Activate
    ActiveSheet.Copy Before:=wb.Sheets(1)
    wb.Activate
    wb.SaveAs "file:///C:\Users\admin\Documents\Vedrova%20Black%20belt%20Toepassingen\Offer.xlsx"

End Sub
Now, If I click on this button in the ribbon, I get the error: "cannot run the macro the macro may not be available in this workbook"

What do I do wrong?

Many thanks in advance for your help.
 
Last edited by a moderator:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Welcome to the forum.

Which module of the workbook is the ModCopySave routine in?
 
Upvote 0
Hi, Thanks for your quick reply.
This module has not been added to a workbook (is this different from a worksheet?)
I assumed that this macro (behind the button in the customized ribbon) would take the active sheet each time to save it, as a new Excel file.
I also added this macro to the Quick Access Toolbar and it does work there .. so a new file is created and saved from the current active sheet.
 
Upvote 0
Where is the macro then? It needs to be in the workbook whose Customui you are editing.
 
Upvote 0
Hi,

I wanted to send you a printscreen, but apparently that did not work out...

I added 2 modules.
- 1 module is the macro: ModCopySave (to save the active sheet in a seperate excel file).
- And module 2 is for the customized Ribbon: ModCustRib.

In (module2) ModCustRib I have added:

'Callback for CopySave onAction
Sub ModCopySave(control As IRibbonControl)
Dim wb As Workbook

Set wb = Workbooks.Add
ThisWorkbook.Activate
ActiveSheet.Copy Before:=wb.Sheets(1)
wb.Activate
wb.SaveAs "file:///C:\Users\admin\Documents\Vedrova%20Black%20belt%20Toepassingen\Offer.xlsx"

End Sub

This is the same as in (module 1) ModCopySave.

First I wrote:

Sub ModCopySave(control As IRibbonControl)
Application.Run "ModCopySace"
End SubBut that didn't work either. So then I wrote the entire macro from module 1 (ModCopySave) in module 2 (ModCustRib).
 
Upvote 0
You should have one module and do not call it ModCopySave. In that module, all you need is:

Code:
'Callback for CopySave onAction
Sub ModCopySave(control As IRibbonControl)
Dim wb As Workbook

Set wb = Workbooks.Add
ThisWorkbook.Activate
ActiveSheet.Copy Before:=wb.Sheets(1)
wb.Activate
wb.SaveAs "file:///C:\Users\admin\Documents\Vedrova%20Black%20belt%20Toepassingen\Offer.xlsx"

End Sub

Make sure you do not have any other routines called ModCopySave in the workbook.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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