Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Run time error 9 (subscript out of range)

I need some assistance with my programm below . I am
getting run time error 9 at the indicated position


Public f_name as variant

Public Sub Main()

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


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

Workbooks.Open
Filename:="c:\switch_makros\Switch_Temp.xls"
Sheets("Sheet1").Select
..
..
..
Call Temp

End sub


Public Sub Temp()
..
..
Windows(f_name).Activate ' I get run time error 9 here
..
..
end sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default Run time error 9 (subscript out of range)

Hi Nathaniel,

The root problem is that the string returned by the GetOpenFilename
method is not the same as the Window name of that file once it has been
opened. Here's one way of rewriting your code that will solve this problem.

Public Sub Main()

Dim f_name As Variant
Dim wkbBook As Workbook

f_name = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If f_name < False Then MsgBox "Open " & f_name

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

Set wkbBook = ActiveWorkbook
Workbooks.Open Filename:="c:\switch_makros\Switch_Temp.xls"
Sheets("Sheet1").Select
Temp wkbBook

End Sub

Public Sub Temp(ByRef wkbBook As Workbook)
wkbBook.Activate
End Sub

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


"Nathaniel Tigere" wrote in message
...
I need some assistance with my programm below . I am
getting run time error 9 at the indicated position


Public f_name as variant

Public Sub Main()

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


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

Workbooks.Open
Filename:="c:\switch_makros\Switch_Temp.xls"
Sheets("Sheet1").Select
.
.
.
Call Temp

End sub


Public Sub Temp()
.
.
Windows(f_name).Activate ' I get run time error 9 here
.
.
end sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Run time error 9 (subscript out of range)

f_name contains the whole path and filename, not just the
filename. You need to strip the path using something like
this:

Do While InStr(f_name, "\")
f_name = Right(f_name, Len(f_name) - InStr
(f_name, "\"))
Loop


-----Original Message-----
I need some assistance with my programm below . I am
getting run time error 9 at the indicated position


Public f_name as variant

Public Sub Main()

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


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

Workbooks.Open
Filename:="c:\switch_makros\Switch_Temp.xls"
Sheets("Sheet1").Select
..
..
..
Call Temp

End sub


Public Sub Temp()
..
..
Windows(f_name).Activate ' I get run time error 9 here
..
..
end sub
.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
run-time error '9': subscript out of range [email protected] uk Excel Discussion (Misc queries) 4 December 8th 09 10:27 PM
Error:Subscript out of range Jay Excel Discussion (Misc queries) 1 April 10th 08 10:25 PM
Run Time Error 9 (Subscript out of Range) for XLA file ExcelMonkey Excel Discussion (Misc queries) 3 October 5th 05 03:34 PM
Subscript out of range error moglione1 Excel Discussion (Misc queries) 2 August 30th 05 01:21 PM
Run time error 9 : Subscript out of range JAtz_DA_WAY Excel Discussion (Misc queries) 6 August 29th 05 08:26 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"