How to virtually click a button in a userform using VBA - excel

I have a userform with 4 buttons.
I want to start the userform with button 1 already active.
The button is a public sub called "StartMeasButton_Click()" and is inside the userform. I need to click on that button.
I tried the Application.Run method but it fails, as I've understood, as the method is for a spreadsheet command button and not for a userform inside button.

Simplest solution: Make your button click call a subroutine, then you can call the subroutine whenever you want to "click" the button.
Sub StartMeasButton_Click()
MySubRoutine
End Sub
Sub MySubRoutine()
'put the body of your click routine here
End Sub
Sub Form1_Initialise
MySubRoutine 'effectively the same as clicking the button
End Sub

Related

Passing information from a UserFrom to another Sub/Function/Module using buttons and the OnClick event

Within a Excel File, I have a UserForm which is opened by clicking on a custom button on the ribbon at the top, where you can select a file from the dropdown menu.
There is a button below the Dropdown, which starts a VBA script that handles that file in a way, that is not relevent to this question.
Now my way of doing it was, assigning a sub to the button on the top of the ribbon, that opens the UserForm, and then assigning another sub to the button, which is called by the inbuilt click event handler.
My question is, is there a better way of dealing with this?
'button onclick code
Private Sub myButton_Click()
Me.Hide
SubDealingWithFile (cmbxFirstExcelfile.Text)
End Sub
'sub on ribbon
Sub RibbonSub(control As IRibbonControl)
Dim form1 As myForm
Set form1 = myForm
form1.Show
End Sub

How can I tie a VBA macro to a button in Excel

I have created a macro and a button but I can't see where to edit the button setting to invoke the macro. I'm using Excel 2003.
If you've used a Form Control. The chance to assign a macro will come up right after you add the button to the sheet. Or, if the button is already on the sheet, you can right-click on it and select Assign Macro.
If you've used an ActiveXControl, right-click on the button and select View Code. This will open up the VBE with
Private Sub myCommandButtonName_Click()
End Sub
From this sub call your macro like so...
Private Sub myCommandButtonName_Click()
Call myMacro
End Sub

Excel VBA - Clicking on the workbook to unload a userform

I have a userform that populates an excel worksheet but allows the tabs at the bottom of the workbook to be displayed. When a user clicks one of the tabs while the userform is opened, nothing happens. Is there a way to unload the userform whenever the user clicks on something that is not a part of the form?
Thanks
Here's one way, although I'm a little skeptical about how well it will work in practice. First set the ShowModal property of the Userform to False. That will allow the user to click outside of the Userform and actually select something.
Next, put this code in the ThisWorkbook module
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
On Error Resume Next
Unload UserForm1
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
Unload UserForm1
End Sub
When a user clicks either a sheet tab or a cell in the active sheet, it will unload the userform. There are probably a hundred other places the user could click that will not fire these events, but it may work for your situation.

Attach Existing UserForm Button to Excel Workbook Sheet

I have an Excel workbook that has a macro (the only macro in the workbook) attached to a button on a sheet.
In VB mode, I created a UserForm under Forms with a CommandButton1_Click Sub and when run from within VB (Run > Run Sub/UserForm or F5) it runs fine. I have it calling a Shell command that runs a BAT file that runs a Python script.
How do I run CommandButton1_Click from a button on a sheet? If I try to add a button to a sheet, it offers me the macro I have already associated with the other button.
Create one macro for example
Sub ShowForm
YourForm.show
End Sub
And associate this macro in button.
Why don't you move the main code into a new macro to modularise it. You can then call your macro via both UserForm and worksheet ActiveX (or Forms) buttons
'Normal Code Module
Sub TestCode()
MsgBox "Hi"
End Sub
'UserForm code
Private Sub CommandButton1_Click()
Call TestCode
End Sub
'ActiveX button code
Private Sub CommandButton1_Click()
Call TestCode
End Sub

How to capture the "Print" button from the menu bar in a macro

I have a sheet with a custom button on it from where I control the printing process.
Now the user clicks on the menu bar's print icon and this produces an "undefined" output.
How can I intercept this menu bar button?
Thanks
Handle the Workbook_BeforePrint event.
private sub Workbook_BeforePrint (cancel as boolean)
'//g_MyFlag is set when the user clicks you toolbar button.
'//It must get cleared in the end of your procedure.
if not g_MyFlag then cancel = true: exit sub
end sub
In MS Word, it's also possible to redefine the system macro itself. You'd have to create a macro named FilePrint(), and Word would call it instead its own. A pity you can't do that in Excel.

Resources