Excel VBA - Clicking on the workbook to unload a userform - excel

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.

Related

Why macros are disabled each time I re-start my Excel workbook, even after enabling content...?

Here the context : I'm working on a local workbook (c:) that directly opens a UserForm within the Workbook_Open() Sub. Then, when UserForm is closed, Excel is also automatically closed. Nothing special there.
The problem : Even with a correct setup in my Trust Center Settings (Disable all macros WITH notification), the warning security message pops up again each time I reload that workbook.
Here my code :
Private Sub Workbook_Open()
' modal display of my form
myUserForm.Show vbModal
' upon return from my form, save and close that workbook
Me.Close savechanges:=True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' also quit Excel app if no more workbook is opened
If Application.Workbooks.Count = 1 Then Application.Quit
End Sub
I'm pretty sure that's related to the fact that Excel is automatically closed by program, and not by user himself.
For example if I reduce the code to the strict minimum, like :
Private Sub Workbook_Open()
myUserForm.Show vbModal ' modal display of my form
End Sub
In that situation, so without any Close or Quit, I just fall into my main worksheet when the UserForm is closed, then I just manually close Excel, with or without saving, and that's it, warning popup will not appear next time I'll open that workbook.
I don't know if there is a solution without playing with Trust Locations or All macros enabled.
Any ideas ?
Thanks for your help

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 to virtually click a button in a userform using VBA

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

Sheet select from a Userform button causes data entered to go on previous sheet

I call a macro called SelectSheet1, from a button on a userform, to select Sheet1.
When data is entered afterwards it is put on the previous sheet.
This is happening on Excel 2013. I confirmed it is not a problem in Excel 2007.
Also it is not a problem if the macro is run directly from the developer tab, keyboard shortcut, quick access toolbar or ribbon customization.
The userform command button code:
Private Sub CommandButton1_Click()
Call SelectSheet1
Unload UserForm1
End Sub
The SelectSheet1 macro selects the sheet:
Sub SelectSheet1()
Sheets("Sheet1").Activate
End Sub
Link to xlsm file in dropbox
Link to youtube video if you want to see with your own eyes
It is a strange error and wondering if it a problem with Excel 2013, something changed in the way they do things and possibly there is a workaround.
Make sure there is only a single sheet Select'ed before you Activate a sheet:
Sub SelectSheet1()
Sheets("Sheet1").Select
Sheets("Sheet1").Activate
End Sub
Remember: "Although only a single sheet may be Active, many may be Selected."
I was able to figure out how to get it to work, but not sure why this solves the issue.
Im unloading the Userform before call macro and I tried using select instead of Activate, those did not help. But after I switched so that the UserForm loads with vbModeless then my problem went away, not sure why this fixed it.
For me the key to fixing this issue was vbModeless, but would love if somebody who understood more could explain why.
Private Sub CommandButton1_Click()
Unload UserForm1
Call SelectSheet1
End Sub
Sub ShowMainMenu()
UserForm1.Show vbModeless
End Sub
Sub SelectSheet1()
Sheets("Sheet1").Select
End Sub

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

Resources