views:

657

answers:

2

I'm Writing a VBA macro in excel that is supposed to open "fileB.xls" and then close immediately after it opens. Since we will be talking about 3 excel files I'll call the first one fileA.

The code for the buttons on the userform for file A look like the following.

Private Sub CommandButton2_Click()  
    'Code for Button in FileA '
    Workbooks.Open Filename:="File Path/fileB.xls"  
    ThisWorkbook.Close  
End Sub

Once "fileB.xls" opens it launches a form to collect some user information.

However the original file does not close after, "fileB.xls" is opened.

Sub Workbook_Open()
    'Code in FileB '
    'Display a form to obtain information from the user.  '
    frmOpenFile.Show  
End Sub

It might be worth noting that fileB opens a third and final excel file (FileC). Code that is similar to FileA's code is used in FileB and work fine when launching FileC (FileB closes fine).

The idea is that there is one "master" file where you choose which form you need to fill out. This master file then closes itself and lets the user fill out the form, and based upon user input a final excel file is opened.

So, just to reiterate the problem, FileA opens, launches a userform, the user clicks on a button, File B lauches, FileA does not close when it should.

I would be grateful for any help!

EDIT: I should note that if rewrite FileA to directly open FileC, FileA closes correctly. Could the user form from FileB prevent File A from closing?

A: 

If your workbook FileA has changed in anyway the user will be prompted to save the workbook.

This might be hidden from view depending on what other code you are running.

To avoid this prompt, so long as you don't need to save the changes, this code will work:

ThisWorkbook.Close SaveChanges = False
Robert Mearns
Thanks for the help. Unfortunately the problem persists, but I really should have it in there, never know what the users might do.
Sumason
if you do need to save changes just change savechanges to true.
guitarthrower
+2  A: 

Yes, it's the userform. The userform in File C is nonmodal. It's the opposite of the one in File B. When File B opens, the userform opens and all code execution stops until it's closed. With File C, code execution continues. If you change File B's userform properties ShowModal to False, they will work the same.

In the VBE, open the Userform in question and press F4 to open the Properties dialog. Find the ShowModal property and change to False.

Dick Kusleika
You, Sir, are a God amongst men. I had been bashing my head against this for most of the morning.
Sumason