3

I'm working on a Macro and want to know about this to avoid any bugs or crashes.

Scenario: I have a file which does all the processing (Macro File). My code opens a pre-existing excel (Template) file then writes some things into it and then Save As's it at particular location. For Opening pre-existing file, I'm using this:

Set SOWbk = Workbooks.Open(FileName:=SOFileName, ReadOnly:=True)

For Saving As this file, I'm using this:

SOWbk.SaveAs FileName:=ThisWorkbook.Path & "\" & TextBox13.Value & "_SO.xlsm", FileFormat:=52

Questions: 1. After Save As, does the original file (Template) remains open and i have to close it anyhow? 2. Does "SOWbk" Object gets the reference of the newly saved as file automatically?

I tried searching this on Google and on Stack as well. I did not found any help on this. Thank you for your help in advance! Much appreciated.

Sandeepr
  • 79
  • 7

1 Answers1

3

Answer to Question 1: Does the old workbook stay open after SaveAs

No. You open Workbook A that is stored in Location A. When you use the SaveAs function, you save the "current Version" of the open Workbook (which is not the same as what is stored in Location A) to Location B. The file that is now open is file B so to say.

Answer to Question 2: What happens to the Workbook Object

The Object refers to what i refered to as Workbook B. The explanation is the same as above. The opened workbook is not a representation of what is stored in Location A.

enter image description here

FloLie
  • 1,820
  • 1
  • 7
  • 19
  • Thank you for the answer! This clarifies my doubt. One last query: After "Saving As", Excel automatically closes the Workbook A (From your example), like Worbook.close? And is that file is completely closed? – Sandeepr Apr 23 '18 at 21:41
  • 2
    It doesn't 'close' the original workbook so much as it breaks the link between the excel application and the stored file in favour of creating a link to the newly created file. –  Apr 23 '18 at 21:45
  • Imagine it like this: Workbook `A` is stored on the disk. When you open the file, a temporary copy of `A` lets call it `A'` is stored in your RAM. When you now `SaveAs`, `A'` is stored to the Disk as `B`. The object in your RAM does not change, but it is now referenced to `B`. – FloLie Apr 23 '18 at 21:45
  • @FloLie Do we have to manually close file A with .close? Or it just not required anymore and it won't affect my code. – Sandeepr Apr 23 '18 at 22:03
  • You don't have to close the file. – FloLie Apr 23 '18 at 22:04
  • Thank you for this answer and graph explanation. Appreciated! :) – Sandeepr Apr 23 '18 at 22:06