ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   check if file is already open (https://www.excelbanter.com/excel-discussion-misc-queries/162062-check-if-file-already-open.html)

mohavv

check if file is already open
 
Hi,

I want a macro to check is a file, which is called upon, is already
open and if so skips the opening line of the code and proceeds with
the rest, otherwise proceed with opening the file.

Currently Excel asks me if I want to reopen the file and unsaved data
will be lost.

Cheers,

Harold


Dave Peterson

check if file is already open
 
Dim Wkbk as workbook
....

set wkbk = nothing
on error resume next
'don't use the drive/path here
set wkbk = workbooks("somename.xls")
on error goto 0

if wkbk is nothing then
'not open, so open it.
'you need the drive/path here
set wkbk = workbooks.open(filename:="C:\my documents\excel\somename.xls")
end if



mohavv wrote:

Hi,

I want a macro to check is a file, which is called upon, is already
open and if so skips the opening line of the code and proceeds with
the rest, otherwise proceed with opening the file.

Currently Excel asks me if I want to reopen the file and unsaved data
will be lost.

Cheers,

Harold


--

Dave Peterson


All times are GMT +1. The time now is 03:35 AM.

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