Problems opening a text file using macro!

  • Thread starter Thread starter aiyer
  • Start date Start date
A

aiyer

Hi all!

I've a quick question with respect to the short macro shown below.

Sub importfile()
Application.DisplayAlerts = False
MyFile = Application.GetOpenFilename("text files,*.txt")
Workbooks.OpenText FileName:=MyFile, _
Origin:=xlWindows, StartRow:=1,
DataType:=xlFixedWidth, FieldInfo:= _
Array(Array(0, 1), Array(11, 1), Array(20, 1))
ActiveWindow.WindowState = xlNormal
Sheets("MyFinal").Select
Sheets("MyFinal").Move Before:=Workbooks
("vtec.xls").Sheets(1)
End Sub


The browsed and selected *.txt file is assigned to the variabl
"MyFile".
However, I am unable to move it before the existing workbook '
tec.xls' and it is giving me error. A small error in the line:

Sheets("MyFinal").Select
Sheets("MyFinal").Move Before:=Workbooks("vtec.xls").Sheets(1)

I would appreciate any tips.
Thanks alot guys.

Regds,

Arun.
Vtec Corp
 
Try opening the workbook manually, then turn on the macro recorder, then move
the sheet to the other workbook. The recorded code will give you the correct
syntax.
 
Your code runs fine for me if
a) I have vtec.xls open
b) the file I select to open is named MyFinal.txt

If you don't meet those two conditions, you will have problems. But since
you wrote it that way, you must be aware of that. If you want it to work
with any text file selected, then

Sub importfile()
Dim bk as Workbook
Application.DisplayAlerts = False
MyFile = Application.GetOpenFilename("text files,*.txt")
set bk = Workbooks.OpenText( Filename:=MyFile, _
Origin:=xlWindows, StartRow:=1, _
DataType:=xlFixedWidth, FieldInfo:= _
Array(Array(0, 1), Array(11, 1), Array(20, 1)))
bk.Sheets(1).Move Before:=Workbooks _
("vtec.xls").Sheets(1)
End Sub
 
Back
Top