ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to test success of a "Set" command? (https://www.excelbanter.com/excel-programming/389547-how-test-success-set-command.html)

[email protected]

How to test success of a "Set" command?
 
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.DefaultFilePa th & _
"\" & 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?


Jim Thomlinson

How to test success of a "Set" command?
 
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
--
HTH...

Jim Thomlinson


" wrote:

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.DefaultFilePa th & _
"\" & 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?



[email protected]

How to test success of a "Set" command?
 
Thanks Jim


Jim Thomlinson wrote:

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



All times are GMT +1. The time now is 12:22 PM.

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