Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / May 2008

Tip: Looking for answers? Try searching our database.

error 9 - simple code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
PBcorn - 30 May 2008 11:36 GMT
to open a  workbook if it is not already open:

dim path as string

path = " letter:\...\\workbook.xls"

If Workbooks(path).IsOpen = False Then
workbooks.open filename:=path

Gives subscript out of range. The solutions i found by searching the forum
are a bit long-winded - why doesn't the above work?
Sam Wilson - 30 May 2008 12:01 GMT
It's because the workbooks collection contains only open workbooks.

You can try the following, which checks all the open workbooks against the
path you've specified:

Sub test()

Dim path As String
path = "C:\blah.xls"

Dim wb As Workbook
For Each wb In Workbooks
   If wb.FullName = path Then Exit Sub
Next wb

Workbooks.Open Filename:=path

End Sub

You should be able to

> to open a  workbook if it is not already open:
>
[quoted text clipped - 7 lines]
> Gives subscript out of range. The solutions i found by searching the forum
> are a bit long-winded - why doesn't the above work?
PBcorn - 30 May 2008 13:39 GMT
This generates a type mismatch error 13. please advise.

> It's because the workbooks collection contains only open workbooks.
>
[quoted text clipped - 28 lines]
> > Gives subscript out of range. The solutions i found by searching the forum
> > are a bit long-winded - why doesn't the above work?
PBcorn - 30 May 2008 13:48 GMT
...which seems to be due to the "if wb.fullname = path" line.

> This generates a type mismatch error 13. please advise.
>
[quoted text clipped - 30 lines]
> > > Gives subscript out of range. The solutions i found by searching the forum
> > > are a bit long-winded - why doesn't the above work?
Sam Wilson - 30 May 2008 13:56 GMT
What version of excel are you using? It mustn't be treating wb.fullname as a
string...

try cstr(wb.fullname) instead? or wb.fullname.tostring

> This generates a type mismatch error 13. please advise.
>
[quoted text clipped - 30 lines]
> > > Gives subscript out of range. The solutions i found by searching the forum
> > > are a bit long-winded - why doesn't the above work?
PBcorn - 30 May 2008 14:13 GMT
Excel 2002 SP3

cstr generates the same error, wb.fullname.tostring generates "invalid
qualifier".

Please advise.

> What version of excel are you using? It mustn't be treating wb.fullname as a
> string...
[quoted text clipped - 35 lines]
> > > > Gives subscript out of range. The solutions i found by searching the forum
> > > > are a bit long-winded - why doesn't the above work?
Sam Wilson - 30 May 2008 14:54 GMT
Can you post your code here so I can have a look?

> Excel 2002 SP3
>
[quoted text clipped - 42 lines]
> > > > > Gives subscript out of range. The solutions i found by searching the forum
> > > > > are a bit long-winded - why doesn't the above work?
PBcorn - 30 May 2008 14:58 GMT
I omitted to mention that i had added an "or" as follows:

If wb.FullName = path2 or path3 Then Exit Sub

which seems to cause the problem. using two if statments is not a solution
as obviously the exit sub of the first negates the second.

I could just write two subs but is there a better way?

> to open a  workbook if it is not already open:
>
[quoted text clipped - 7 lines]
> Gives subscript out of range. The solutions i found by searching the forum
> are a bit long-winded - why doesn't the above work?
Sam Wilson - 30 May 2008 15:00 GMT
Rather than "If wb.FullName = path2 or path3 Then Exit Sub"

use "If wb.FullName = path2 or wb.fullname = path3 Then Exit Sub"

> I omitted to mention that i had added an "or" as follows:
>
[quoted text clipped - 16 lines]
> > Gives subscript out of range. The solutions i found by searching the forum
> > are a bit long-winded - why doesn't the above work?
Dave Peterson - 30 May 2008 14:58 GMT
Dim myPath as string
dim myFileName as string
dim wkbk as workbook

mypath = "C:\something\"
if right(mypath,1) <> "\" then
 mypath = mypath & "\"
end if

myfilename = "workbook.xls"  '<-- don't include the path here!

set wkbk = nothing
on error resume next
set wkbk = workbooks(myfilename)
on error goto 0

if wkbk is nothing then
 on error resume next
 set wkbk = workbooks.open(filename:=mypath & myfilename)
 on error goto 0
end if

if wkbk is nothing then
 msgbox "Not open and can't find it!"
else
 msgbox wkbk.fullname
end if

This assumes that C:\something\

> to open a  workbook if it is not already open:
>
[quoted text clipped - 7 lines]
> Gives subscript out of range. The solutions i found by searching the forum
> are a bit long-winded - why doesn't the above work?

Signature

Dave Peterson

Dave Peterson - 30 May 2008 15:13 GMT
I was going to say that "This assumes that C:\something\workbook.xls" exists.

Then I changed the code to try to open it and report back if it fails.  Ignore
that line in the original post.

> Dim myPath as string
> dim myFileName as string
[quoted text clipped - 41 lines]
>
> Dave Peterson

Signature

Dave Peterson

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.