How to test success of a "Set" command?

  • Thread starter Thread starter EagleOne
  • Start date Start date
E

EagleOne

2003/2007

There is a probable a better way. The GoToWb opens at the '******* line of code. If the file
has not opened by '$$$$$$$$$ line then I have the code line -> If Err.Number > 0 Then

Workbooks(GoToWb).Activate If Err.Number > 0 Then
On Error GoTo 0
On Error Resume Next
Set Windows(GoToWb) = Workbooks(GoToWb)
On Error GoTo 0
On Error Resume Next
Set Workbooks(GoToWb) = Workbooks.Open(Filename:=GoToPath & GoToWb) '*******
On Error GoTo 0
On Error Resume Next
Set Workbooks(GoToWb) = Workbooks.Open(Filename:=Application.DefaultFilePath & _
"\" & GoToWb) '$$$$$$$$$
If Err.Number > 0 Then
.......
Endif
Endif

Since the file opens at '******* then the Set command at '$$$$$$$$$ produces an error when
the file is already open.

How do I test the success of the previous Set command so that I do not produce an error?
 
Your code has me at a bit of a loss so here is a generallized method...

dim wbkOpen as workbook

on error resume next
set wbkOpen = workbooks("MyBook.xls")
on error goto 0

is wbkOpen is nothing then _
set wbkOpen = Workbooks.Open("C:\Mybook.xls")

'Note that Nothing is the test for an object being initialized
 

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