VBA: Opening and closing workbooks within workbooks

jst3712

New Member
Joined
Apr 15, 2010
Messages
10
I have an Excel workbook (using VBA for Excel) with a macro that opens another workbook, then after a small delay, the original workbook closes itself (as it's no longer needed). But something is not quite right with the code...

Code I have in a workbook called "workbook1":

Sub sub_OpenFolder()

Dim openme As String

openme = "c:\workbook2.xlsm" 'sample file only
Workbooks.Open openme 'open second workbook (works fine)


Dim A, B, C, D
A = 2 'Delay in seconds (works fine)
B = Timer
Do While Timer < B + A
DoEvents
Loop
C = Timer
D = C - B

ThisWorkbook.Close SaveChanges:=False 'Close this workbook but don't save (problem here ****)

End Sub



The original workbook ("workbook1") should open the second workbook ("workbook2"), and then workbook1 should close itself after a few seconds. But with the above code, why does "workbook2" close as well? :confused:

I thought "ThisWorkbook.Close" would cause the original workbook to close itself - which it does - but it's also closing workbook2.xlsm for some reason. Can't it tell the difference between "this" workbook from the "other" workbook?

Please note: The file names and data paths vary all the time, so I cannot hard-code a particular data path and file name which makes it trickier.

Any help will be much appreciated. Thank you.
JASON.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Do you have any code in the BeforeClose event of the first workbook?
 
Upvote 0
Do you have any code in the BeforeClose event of the first workbook?

Ah-haaaa!!!! I forgot all about the 'Application.Quit' I had in that section after a MsgBox (reminder message). I had it there to force Excel to shut down after a potential general error, but I will need to make some changes.

I'm sure that the removal of Application.Quit will solve the problem of both workbooks closing... I'll try it tomorrow.

Thanks! So quick in replying too :)
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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