Restart Current UserForm - excel

I want to restart the master userform for a project I'm working on excel.
I tried hide, unload other userforms, call UserForm_Initialize and show again but not worked. Some variables still in memory, close (X) button didn't work anymore etc.
I looked on internet and there is no solution . So i found that solution below.
I added another empty master userform and call my userform if user or my codes unload or close my userform, master userform automaticly show again. This works but i cant use vbModeless Because out of stack error
i wonder is there another (more proper) solution ?
my master userform codes:
Option Explicit
Private Sub UserForm_Click()
StartApp 'there is no escape :)
End Sub
Private Sub UserForm_Initialize()
StartApp
End Sub
Public Sub StartApp()
Me.Hide 'sometimes this form showing itself so i added everywhere :)
Splash.Show
Me.Hide
StartApp
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = True
StartApp
End Sub
Splash is my loader form. Load's other forms etc.
Edit 1: Solved vbModeless problem with this link
Still looking another (more proper) solution ? (Without master form)

Related

Why does OnTime not execute when a userform is open?

I am attempting to use a self-activating OnTime sub to edit some text in a UF control. However, I find that an Application.OnTime event won't start working until the UserForm is closed.
As per this SO thread, I have placed the self-activating sub in a public sub in a regular module. However, this is to no avail.
UF sub
Private Sub UserForm_Initialize
TEST.loadingdots
End Sub
Regular sub
Public Sub loadingdots
Debug.Print 4
Application.OnTime Now + TimeValue("00:00:02"), "loadingdots"
End Sub
The first "4" gets printed, then nothing. When I close the UF, the procedure gets executed as expected.
How can I work around this?
Answering my own question for future reference
When a UF holds a RefEdit control, an OnTime event won't be executed.
I created a new, blank UF, which caused no issues whatsoever.
Seems like RefEdits need to be approached with caution, similarly to how using a RefEdit control in a modeless userform forces the user to close the Excel application through task manager.

UserForm Close button fails after form launched for second time

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

Cannot reach vba code after Userform X-Button modification

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

How to trigger an event after the user lose focus on a?

I am new to visual basic. I have a TextBox, and I want to trigger an event when the user lose focus on the textBox.
I tried writing
Private Sub TextBox_LostFocus()
something
End Sub
and
Private Sub TextBox_Leave()
something
End Sub
I don't really understand how they work, I have
Private Sub TextBox_Change()
something
End Sub
and that works fine, so what am I missing? How do I trigger the event when the user is not writing in the textbox anymore?
This is what you want:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
End Sub
There's a helper -- when you're in the code editor, you'll see two list boxes above the editor. The list on the left contains the available objects for the current module. Select TextBox from there if it isn't already selected. The list box on the right contains the available Events. You should see Exit in there. Clicking that will paste in the code above.

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.

Resources