UserForm Close Event Hangs

trough

Board Regular
Joined
Oct 26, 2010
Messages
55
Through use of multiple UserForms I open a 2nd workbook that I hide and then may get or write data to that workbook before closing it. All was good until I noticed that when the red X in upper right corner (Close event) of forms was clicked that the 2nd workbook was left open and hidden.

To solve this issue I added the UserForm_QueryClose subroutine to each UserForm. It works perfect for the initial UserForm but when used in the 2nd UserForm Excel completely hangs. I have to use the Task Manger to end Excel.

I cannot figure out the issue. Seems something is still open and when I close the form all goes South. Am I doing something in the wrong order? Please advise. Thanks.



'launch initial UserForm:
Sub GetForm()
Frm1.Show
End Sub

'Frm1 UserForm:
Private Sub UserForm_Initialize()
'open database workbook and hide
Workbooks.Open Filename:=g_path & "\" & g_file
Windows(g_file).Visible = False
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
'close Form1
Unload Frm1

'close the database workbook
Workbooks(g_file).Activate
Windows(g_file).Visible = True
ActiveWorkbook.Close SaveChanges:=False
End If
End Sub

Private Sub CmdOk_Click()
Workbooks(g_file).Activate
'launch Form2
Frm2.Show
End Sub



'Frm2 UserForm:
Private Sub UserForm_Initialize()
'close initial UserForm
Unload Frm1
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
'close Form2
Unload Frm2

'close the database workbook
Workbooks(g_file).Activate
Windows(g_file).Visible = True
ActiveWorkbook.Close SaveChanges:=False
End If
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top