VBA Help Needed - User form closed when other excel workbook is closed

MistakesWereMade

Board Regular
Joined
May 22, 2019
Messages
103
So I have a form where its workbook is hidden. The user opens a file which generates a separate open workbook. My problem is when I close this other workbook, excel prompts me to save/unsave my userform too...

I've included some code in the ThisWorkbook module that allows me to close a separate workbook once without it prompting me about my userform. But when I open a second workbook and try to close it... It then asks me to close my userform. I want to be able to freely open and close as many separate workbooks as I want to without excel prompting me to close my form.

Any ideas?

I've included the current code from the ThisWorkbook Module...

Code:
Public swb As String
Private Sub Workbook_Open()


    Application.ScreenUpdating = False
    swb = ThisWorkbook.Name
    ThisWorkbook.Application.Visible = False
    UserForm1.Show vbModeless
    Application.ScreenUpdating = True


End Sub


Private Sub Workbook_BeforeClose(cancel As Boolean)


    If swb <> ActiveWorkbook.Name Then
        ActiveWorkbook.Close
        Application.Visible = False
        UserForm1.Show vbModeless
    Else
        ThisWorkbook.Close
    End If


End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Code:
[LEFT][COLOR=#000000][FONT=monospace]Application.DisplayAlerts = [/FONT][/COLOR][COLOR=#07704a][FONT=monospace]False
[LEFT][COLOR=#333333][FONT=monospace]ActiveWorkbook.Close[/FONT][/COLOR]
[/LEFT]
[FONT=monospace]Application.DisplayAlerts = [/FONT][COLOR=#07704a][COLOR=#07704a][FONT=Verdana][FONT=monospace]True[/FONT][/FONT][/COLOR][/COLOR]<strike></strike>
[/FONT][/COLOR][/LEFT]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,445
Messages
6,172,176
Members
452,446
Latest member
walkman99

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