macro closes active workbook but other workbooks wont close

Aphasial

New Member
Joined
Aug 16, 2015
Messages
10
Hi all

I am fairly new to VBA coding and I have found this site very useful for helping me to understand and fix previous problems I have had. I cannot find an answer for my current problem, however, despite extensive internet searches. I have created a userform that allows users to scrape information from a database and then fill out one of several Microsoft word documents based on the users choices from the userform.

This all works fine but the userform has an exit control button for the user to exit the workbook once they are finished. Due to the nature of our work, the user often has several other workbooks open at the same time.

My problem is that after the user exits the userform's workbook, the other workbooks that are open refuse to close, either by clicking on the X or by using file - close

I have built a much simpler version of my userform to test for ways to resolve this but I am stumped

Here is the code I am using in my test userform as I cannot share the original.

Code:
Sub StartUserform()

'calls userform

     UserForm1.Show

End Sub

Private Sub push_Click()

'simple test for other option on userform

      Call TestMacro

End Sub

Private Sub ExitButton_Click()

'exit button on userform

      Unload Me
         Call CloseBook

End Sub

Sub TestMacro()

'for userform option 1

     MsgBox "It Works!!"
   
End Sub

Sub CloseBook()

'to exit workbook

      Windows("HO.XLSX").Close

End Sub

Sorry for all the little comments in the code, this is to help me remember what everything does as I learn

Thanks in advance
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi Diddi.

Thanks for your suggestion.

When I tried Application.Quit, it closed all excel workbooks and not just the one I want to close. I need to close the specific workbook and leave the others to be worked on and closed separately when they are finished with (sorry I didn't make that clear in my original post:oops:)
 
Last edited:
Upvote 0
Unloading a userform isn't done completely until the Exit Sub is executed.

Since the user form is in the workbook being closed, there you've put the system in a limbo state where

It is in the process of unloading a user form, that is in a workbook, that is now closed.

Try putting this sub in a normal module and calling it from your user form code.

Code:
Sub CloseAndUnload()
    Unload UserForm1
    Me.Close
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,821
Members
449,049
Latest member
cybersurfer5000

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