PDA

View Full Version : Solved: Close and re-open



xcleng184
06-11-2012, 07:06 AM
Can't figure this out:

Need a macro to close a workbook without saving and reopen it (read only). From what I was playing with, it appears that when the workbook closes, it kills the ability of the macro to finish (leaving me with just a closed workbook).

Thought maybe I needed to make a macro in another book but I don't know how to reference that or make that work.

Any suggestions or examples would be appreciated.

Thanks much,

(xcleng184)

CodeNinja
06-11-2012, 07:24 AM
Try this... Please note, it will not save it's self, and you will loose all data with that workbook that you have input... but I think that is what you want anyway...

Lol, and it will not save the macro if you haven't saved it already...





Dim sPath As String
Dim sName As String
sName = ThisWorkbook.Name
sPath = ThisWorkbook.Path
ThisWorkbook.Saved = True
Workbooks.Open Filename:=sPath & "\" & sName

CodeNinja
06-11-2012, 07:28 AM
Oops, forgot your read-only... you can add that in like this:



Sub test()


Dim wb As Workbook
Dim sPath As String
Dim sName As String
sName = ThisWorkbook.Name
sPath = ThisWorkbook.Path

ThisWorkbook.Saved = True
Workbooks.Open Filename:=sPath & "\" & sName, ReadOnly:=True

End Sub

xcleng184
06-11-2012, 07:32 AM
Damn, that worked like a charm! I had been trying to make workbook objects or references to tell it to close, and then tell it to open.

Thanks much!

:clap:

CodeNinja
06-11-2012, 07:34 AM
Awesome. Glad to be of help. Please mark this thread as solved and have a great day.