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?
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.
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?
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.