Excel 2010 Macro Buttons on all worksheets of file
Hi
I can create a Macro button on a particular sheet of an Excel 2010 file, no problems.
But, is there a way to have the button display on all current and future sheets of the file (I need to import new sheets from another source as well as the current ones)?
The macros are recorded to the particular file - I don't want to save them to the Personal Workbook file or the Excel program.
Gail,
Here is a macro that will check each worksheet when activated to make sure it contains a command button named MacroButton. If not, a Forms control command button is added on top of cell G1. The new buttons are linked to a macro called MyMacro.
No matter how you add a worksheet to the workbook, it will have a command button added to it by the macro.
'This code must be installed in ThisWorkbook code pane. It won't work at all anywhere else!Private Sub Workbook_SheetActivate(ByVal Sh As Object)Dim shp As ShapeDim ws As WorksheetWith Sh On Error Resume Next Set shp = .Shapes("MacroButton") On Error GoTo 0 If shp Is Nothing Then Set shp = .Shapes.AddFormControl(xlButtonControl, .Range("G1").Left, .Range("G1").Top, 100, 23) shp.Name = "MacroButton" shp.TextFrame.Characters.Text = "Run macro" shp.OnAction = "MyMacro" End IfEnd WithEnd Sub
Another approach is to add the button to the ribbon. If you do, the button will be there for all worksheets when that workbook is active, then disappear when you work on other workbooks. The biggest advantage of this approach is that the button is always visible, no matter where you scroll on the worksheet.
To create a button on the ribbon, you'll need to edit the CustomXML part of the workbook. You can find a tool to do so on Andy Pope's web page: http://andypope.info/vba/ribboneditor_2010.htm
If it's only for your use you could also customise the Quick Access Toolbar and add the macro button to it, specifying that it should only be available to that workbook.
The problem is that it has to be all "self-contained" within the file (as it is a file that has to go to various computers), and it has around 30 macros!!
Is the Andy Pope Add-in only necessary for my computer while I am creating the custom Ribbon, or would the Add-in have to be on the other computers also??
Alternatively, you could have a User Form containing buttons for each macro.
The User Form can have multiple tabs so you wouldn't necessarily have to have all 30 buttons on one form. The form can be set to be visible at all times once the workbook is opened or on activation of specific sheets if the macros only need to be available on certain sheets.
OK, understand, but thanks all the same as I think yours was probably the best!!
G
Microsoft Excel
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
Here is a macro that will check each worksheet when activated to make sure it contains a command button named MacroButton. If not, a Forms control command button is added on top of cell G1. The new buttons are linked to a macro called MyMacro.
No matter how you add a worksheet to the workbook, it will have a command button added to it by the macro.
Open in new window
Note that the macro must be installed in the ThisWorkbook code pane. It won't work at all if installed anywhere else.Brad