View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Run time error 9

filetoopen contains a fully qualified path and is not a legal argument to
the workbooks collections

as an example:
filetoopen: "C:\my documents\myfile.xls"

workbooks("myfile.xls") is what is required.


Here is one work around:

Public filetoopen as variant

sub open()
filetoopen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If filetoopen < False Then
MsgBox "Open " & filetoopen
End If


Workbooks.OpenText Filename:=filetoopen , Origin:= _
xlWindows, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:=Array(Array(0, _
2), Array(17, 2), Array(30, 2), Array(39, 2))

' reset the value of filetoopen here
filetoOpen = ActiveWorkbook.Name

Call subroutine

End sub

Sub subroutine()

Windows(filetoopen).activete 'I get run time error here

end sub

--
Regards,
Tom Ogilvy


Nathaniel Tigere wrote in message
...
I am writing a macro using getopenfilename to open the
file I want. The procedure works and it opens the file I
want. But when I want to use the file I have opened in
another Sub by calling the sub routine I get get run
time error 9 (Subscript out of range) when I use
windows(filetoopen).Activate to activate the file I
opened in my subroutine

My program looks as follows


Public filetoopen as variant

sub open()
filetoopen = Application _
.GetOpenFilename("Text Files (*.txt), *.txt")
If filetoopen < False Then
MsgBox "Open " & filetoopen
End If


Workbooks.OpenText Filename:=filetoopen , Origin:= _
xlWindows, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:=Array(Array(0, _
2), Array(17, 2), Array(30, 2), Array(39, 2))

Call subroutine

End sub

Sub subroutine()

Windows(filetoopen).activete 'I get run time error here

end sub