Referencing workbooks in macros

  • Thread starter Thread starter Neil
  • Start date Start date
N

Neil

Hi All,

I havent worked with opening and closing workbooks from excel macros for a
while and seem to recall a neat little routine to capture whether a workbook
was open or not, if not it opens it, if open, it continues the macro.

Can someone assist with code to this affect or point me in the direction of
a helpful site.

Thanks.

Neil
 
Hi Neil,
There may be better ways but this works.

Sub Macro1()

On Error GoTo openWorkbook
Windows("Book3").Activate
On Error GoTo 0

MsgBox "Your code after workbook is activated goes here"

GoTo skipOpenWB 'skip the open work book code when
'procedure completes. End also works.

openWorkbook:
Workbooks.Open Filename:= _
"C:\Users\Peter\Documents\Excel\Test Macros\Book3.xlsm"
MsgBox "Workbook had to be opened"
Resume Next

skipOpenWB:
End Sub

Regards,

OssieMac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top