I have a sheet that has a ListBox. The sheet code has a ListBox_DblClick sub that launches UserForm1. UserForm1.CommandButton1 launches UserForm2. The user may input data in UserForm2 then press UserForm2.CommandButton1 which launches a sub in a module (yet to be written) and closes UserForm2.
With the code below, both UserForm2 and UserForm1 close. I just want UserForm2 to close upon click of UserForm2.CommandButton1.
Any thoughts on what I'm doing wrong are appreciated.
UserForm2 code
Private Sub CommandButton1_Click()
Call UpdateBids(TextBox1.Object.Value, TextBox2.Object.Value, ComboBox1.Object.Value)
Unload Me
End Sub
Upon researching this again, I stumbled upon this post:
Why, after launching a userform from a userform, does unloading the 2nd userform close both?
In short it suggested closing Excel then adding DoEvents following UserForm2.Show. It worked. Now only my UserForm2 closes. I've not had the problem since.
Related
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
I have created a Userform, currently I am using this code to hide Excel and just show the userform (Code was place in Workbook)
Private Sub Workbook_Open()
Application.Visible = False
ricwin.Show
End Sub
The issue I am facing is that in ocassions the excel file will open but not as macro enable, and I require to close it and reopen it so the macro runs.
Also is there a chance to display a taskbar icon for the userform without an excel file being in background?
You can fix the problem by adding a ToggleButton to the userform. When the workbook is opened, the worksheet is hidden, only the userform is shown. For this, the following codes are added to a module:
Sub Auto_Open()
Application.Visible = False
UserForm1.Show
UserForm1.ToggleButton1.Value = False
End Sub
enter image description here
Source of sample file
I have a UserForm, A, with a command button that opens UserForm B using the below code:
Private Sub cmd_click()
Me.Hide
B.Show
End Sub
B, when closed via the X button, runs the following:
Private Sub UserForm_Terminate()
Unload Me
A.Show
End Sub
After:
opening B (from A)
closing B and returning to A
and then opening B again
The close button on B ceases to work.
UserForm A is a menu form that leads to several others, so navigating back and forth between the menus is a pretty basic requirement. This behaviour is reproducible for every form linked from A, as well as for tertiary forms that open from those.
Given my target demographic is of the older generation, I want my UserForms to be intuitive to use, which means preserving regular close button functionality.
Does anyone have any info on this? It may be that my google-fu is lacking, but I can't seem to find anyone who has had the same issue. Any input at this stage is greatly appreciated!
Use a variable to refer to the 2nd Userform in the first as this gives you greater control over the second forms status - you can unset the variable after you have closed the 2nd form and returned to the event handler that instantiated it in the first place. This guarantees that each time you click the button to launch UserForm2 that you are working with a 'fresh copy'.
Also you should use the QueryClose event - see MSDN. This event:
Occurs before a UserForm closes
You need to be able to be able to control the re-activation of UserForm1 before the form is disposed.
The Terminate event is slightly different:
Occurs when all references to an instance of an object are removed from memory by setting all variables that refer to the object to Nothing or when the last reference to the object goes out of scope.
In the sample code below I am getting the correct behaviour using a reference to UserForm2 and handling QueryClose.
In this sample code I am using UserForm1 and UserForm2 but you can substitue for A and B in your app:
Code in UserForm1:
Option Explicit
Private m_frm2 As UserForm2
Private Sub CommandButton1_Click()
Me.Hide
Set m_frm2 = New UserForm2
m_frm2.Show
Set m_frm2 = Nothing
End Sub
Code in UserForm2:
Option Explicit
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
UserForm1.Show
Unload Me
End Sub
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
I inserted
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
unload me
thisworkbook.close
End Sub
into my userform1. I actually have 3 buttons on that form but after inserting the code above the workbook closes after clicking on any of these buttons. Any idea how i can reach my vba-editor now?
Ok, as far as I understand you created macro in userform which always closes this workbook - pretty unlucky :)
It is easy way out of it. Turn on VB Editor in any Excel file and run this code:
Application.EnableEvents = False
This will turn off of executing any events, so it will not close your workbook anymore. After you are done editing your code you can easily turn it on by running this code:
Application.EnableEvents = True