LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Import text file without specifying a path macro won't work

I have been asked to write a macro that will import a text file into Excel (Office 2000) so that I can manipulate the data later. I used the macro recorder, and got a general idea of what I need to do, combined with topics in this forum. I put together this little macro, but I can't get it to work. The first part works great; it lets me choose the text file to import. The second part throws a "Run Time Error 1004: Application Defined or or object-defined error". The directory I am trying to pull data from is C:\Macros\Datafiles, and usually more than one text file exists in that directory, hence the need for the user to select the file. I have been playing around with statements for the past few days but cannot fix the problem. Can somebody please help? TIA

Sub GetFile()
Dim fName As String
fName = Application.GetOpenFilename(FileFilter:="Text Files (*.txt),*.txt")
If fName < "False" Then

With ActiveSheet.QueryTables.Add(Connection:=fName, Destination:=Range("A1"))
.Name = "DATA"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "="
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2)
.TextFileFixedColumnWidths = Array(8, 13, 7, 11, 16)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If
End Sub
 
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
File Path - Macro Max2073 Excel Worksheet Functions 1 December 6th 09 11:57 AM
Auto PRINT file path & Date on Excel work sheet ? Harsh Excel Worksheet Functions 1 October 24th 08 04:18 PM
Formula does not work after import of .txt file James Excel Discussion (Misc queries) 1 July 2nd 08 02:36 PM
Skipping Import Text File dialog in a Macro Leon Excel Discussion (Misc queries) 5 January 21st 08 09:12 PM
Formula too long - new file path is shorter than old file path - Excel 2003 Greg J Excel Worksheet Functions 1 November 22nd 06 05:16 PM


All times are GMT +1. The time now is 10:30 AM.

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

About Us

"It's about Microsoft Excel"