Need to make sure workbook is open before proceeding

M

Munchkin

Can you help - Users of my form need to make sure Doc2 is open before running
the macro. If Doc2 is not open I need the macro to stop and a message box to
pop up stating Doc2 is not open.


Application.WindowState = xlMinimized

Windows("@Doc1.xls").Activate
Range("H2").Select
Selection.Copy

Windows("@Doc2.xls").Activate
Range("E1").Select
ActiveSheet.Paste
 
D

Dave Peterson

Dim Doc2Wkbk as workbook

set doc2wkbk = nothing
on error resume next
set doc2wkbk = workbooks("doc2.xls") 'no drive and no path
on error goto 0

if doc2wkbk is nothing then
msgbox "It's not open"
exit sub
end if

=========
Maybe you could have your code just open it (if you know where it is):

if doc2wkbk is nothing then
on error resume next
'include drive and path on the next line
set doc2wkbk = workbooks.open(filename:="c:\path\doc2.xls")
on error goto 0

if doc2wkbk is nothing then
msgbox "it's not open and I don't know where it is!
exit sub '????
else
msgbox "it was opened, but now it is!"
end if
end if
 

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

Top