ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Referencing workbooks in macros (https://www.excelbanter.com/excel-programming/395423-referencing-workbooks-macros.html)

Neil[_5_]

Referencing workbooks in macros
 
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



OssieMac

Referencing workbooks in macros
 
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

"Neil" wrote:

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





All times are GMT +1. The time now is 04:20 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com