ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Need to make sure workbook is open before proceeding (https://www.excelbanter.com/excel-programming/439978-need-make-sure-workbook-open-before-proceeding.html)

munchkin

Need to make sure workbook is open before proceeding
 
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


Range("H2").Select
Selection.Copy


Range("E1").Select
ActiveSheet.Paste


Dave Peterson

Need to make sure workbook is open before proceeding
 
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



Munchkin wrote:

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


Range("H2").Select
Selection.Copy


Range("E1").Select
ActiveSheet.Paste


--

Dave Peterson


All times are GMT +1. The time now is 12:17 AM.

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