Excel CustomUI Button Links - excel

Ok, I know this is an ongoing problem, and I have searched seemingly everywhere for an answer. I apologize if I somehow missed an answer on here, so if you know of one, please advise!
I created a Custom Ribbon in Excel using the 'CustomUI Editor". My macros currently only run from buttons directly on the sheet, not on the UI. I read that it is because of the links to the original file. So I created a fresh new workbook, created new macros (not imported), created custom UI buttons, and linked the macros to the new buttons. The buttons still give me the error that the macro cannot be run and may not be available. What could I be missing here? I have also read that its possible to create a macro to update the links when the workbook is opened. Has anyone had success with this? I would love to have buttons on the ribbon and remove them from my sheet! Thanks for any help that you can provide!

You can use the following sub to add custom menu items. Just replace the .OnAction value with the names of the macros you'd like the control to run.
Sub AddCustomMenu()
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl
'Delete the menu item if it already exists. Use On Error Resume Next in case it doesn't exist.
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("&My Tools").Delete
On Error GoTo 0
Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar") 'Set a CommandBar variable to Worksheet menu bar
iHelpMenu = cbMainMenuBar.Controls("Help").Index 'Return the Index number of the Help menu. We can then use this to place a custom menu before.
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup, Before:=iHelpMenu) 'Add a Control to the "Worksheet Menu Bar" before Help.
cbcCutomMenu.Caption = "&My Tools" 'Give the control a caption
'Working with our new Control, add a sub control and give it a Caption and tell it which macro to run (OnAction).
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Run Macro 1"
.OnAction = "Macro1"
End With
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Run Macro 2"
.OnAction = "Macro2"
End With
End Sub
If you'd like it to add your custom menu whenever you open the workbook, go to the ThisWorkbook module and add the following sub:
Private Sub Workbook_Open()
AddCustomMenu
End Sub

Related

How to access a VBA Userform Button control code programmatically

I want whenever I click on my ActiveX command button on Sheet1 it open VBA Editor and jump directly to the code I wrote for a button on my userform. I know the way to jump directly to code for a module but I couldn't find a property for userform button control (something like ThisWorkbook.VBProject.VBComponents("UserForm1").Controls("MyButton"))
Following is my code for jumping right into my Madule1 code:
Private Sub CommandButton1_Click()
Dim Wb As Workbook: Set Wb = ThisWorkbook
'---Open Module1
Application.VBE.MainWindow.Visible = True
Wb.VBProject.VBComponents("UserForm1").Activate
End Sub
I thaught these pictures would help to better understand my goal:
1-I have an ActiveX command button on sheet1
2-I have userform with a commandbutton on it
3-This commandbuttun has it's own code which I want to jump to it's code
If you want to jump to that specific sub directly, try this:
Private Sub CommandButton1_Click()
Application.VBE.ActiveVBProject.VBComponents("UserForm1").CodeModule.CodePane.Show
With Application.VBE.ActiveCodePane.CodeModule
lStartLine = .ProcStartLine("CommandButton1_Click", 0) '"CommandButton1_Click" being the name of the sub you want.
.CodePane.SetSelection lStartLine, 1, lStartLine, 1
End With
End Sub
First line should show you the code panel for the userform, and the second part selects the row down by the sub you are looking for by name.

How to save a checkbox to a userform which was created programmatically in vba-excel

I created a checkbox for an existing userform in excel via an active X command button which i programmed to keep record of databases of info, however soon after the checkbox disappears on adding another set of information as well as the checkbox-name does not even appear.
Sub Checkbox()
Dim chkBox As Control
Set chkBox = Te.Controls.Add("Forms.CheckBox.1", "CheckBox" & Range("D3").Value)
End Sub
Any suggestions will be greatly appreciated
End Sub

VBA does not active

I suggest that my workbook contains a VBA code below. But It does not run when I opened my workbook too.
Sub Appl_StatusBar()
Application.StatusBar = "SupportABC"
End Sub
If you put your code in the Workbook Open Event it will do what you need. To do this click the top dropdown where it says "(General)" and hit "Workbook". In the right dropdown select "Open" and save your code there". See below
Private Sub Workbook_Open()
Application.StatusBar = "SupportABC"
End Sub

How do I delete all buttons on a worksheet?

I've got a bit of code that creates a Save button on a worksheet (held in the wsReport variable), but it doesn't remove previous buttons. Over time, they tend to build up. Is there any way to do something like this?
wsReport.Buttons.All.Delete
(Not right, obviously, but gives an idea of what I'm looking for.)
See code below :)
Sub RemoveButtons()
Dim i As Integer
If ActiveSheet.ProtectContents = True Then
MsgBox "The Current Workbook or the Worksheets which it contains are protected." & vbLf & " Please resolve these issues and try again."
End If
On Error Resume Next
ActiveSheet.Buttons.Delete
End Sub
Source:
http://www.mrexcel.com/forum/excel-questions/609668-delete-all-buttons-sheet-visual-basic-applications.html
or could you use code below:
(Buttons in VBA are in the Shapes collection).
Sub DelButtons()
Dim btn As Shape
For Each btn In ActiveSheet.Shapes
If btn.AutoShapeType = msoShapeStyleMixed Then btn.Delete
Next
End Sub
source:
Deleting a collections of VBA buttons
See the code below:
Sub DeleteAllShapes()
ActiveSheet.Shapes.SelectAll
Selection.Delete
End Sub
If somebody found this question, but needs to delete only one button, this helped me:
ActiveSheet.Shapes("my_button_name").Delete
There's an easier method that doesn't use VB:
Click the Developer tab
Click Design Mode
Click on any button in the worksheet
Press Ctrl + A to select all buttons in the worksheet
Press Delete

Write Excel Addin with VBA and then Put a button that trigger it

I have written a simple excel add-in with VBA, it contains a form and related codes. after saving it as add-in and then installing it in Excel, nothing happened!
I need to put a button somewhere in ribbons that trigger my add-in, you know like "Solver".
I really need it, pleas tell me how.
I really appreciate any suggestions.
Try this, needs to be added to your add-in, either in a module or in ThisWorkbook.
Private Const Button as String = "SomeName"
Sub Auto_Open 'Or Private Sub Workboo_Open() in ThisWorkbook
Dim CmdBar as CommandBar
Dim CmdBarMenu as CommandBarControl
Dim CmdBarMenuItem as CommandBarControl
Set CmdBar = Application.CommandBars("Worksheet Menu Bar")
Set CmdBarMenu = CmdBar.Controls("Tools") ' Index 6
On Error Resume Next
Application.DisplayAlerts = False
CmdBarMenu.Controls(Button).Delete 'Just in case a button with same name already exists
Application.DisplayAlerts = True
On Error GoTo 0
Set CmdBarMenuItem = CmdBarMenu.Controls.Add(Type:=msoControlButton)
With CmdBarMenuItem
.Caption = Button
.OnAction = "MainSub" 'Name of the sub it will call when button is pushed
End With
End Sub
Make sure you delete the button on closing Excel, otherwise an additional one will be added everytime you open your addin.
Sub Auto_Close 'Or Private Sub Workbook_BeforeClose(Cancel As Boolean) in ThisWorkbook
Dim CmdBar as CommandBar
Dim CmdBarMenu as CommandBarControl
Set CmdBar = Application.CommandBars("Worksheet Menu Bar")
Set CmdBarMenu = CmdBar.Controls("Tools") ' Index 6
On Error Resume Next
Application.DisplayAlerts = False
CmdBarMenu.Controls(Button).Delete
Application.DisplayAlerts = True
On Error GoTo 0
End Sub
Your sub that you have created that you wish to run from button.
Public Sub MainSub
MsgBox("Hello")
End Sub
You can also add a list box in the Add-in ribbon to hold multiple buttons. First create a MenuItem as type:=msoControlPopup then within the popup add buttons as above.
As well as this VBA code, it is much easier for you to go File -> Options -> Customize Ribbon and add a new tab with a new group and assign a macro to that group. But that will only work for you, the above code will allow anyone to install the addin and have a button automate upon opening.
Hope this answers your question.

Resources