ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Run time error 9 (https://www.excelbanter.com/excel-programming/273179-re-run-time-error-9-a.html)

Tom Ogilvy

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





All times are GMT +1. The time now is 09:50 AM.

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