Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Import text file without specifying a path macro won't work

Does it ned to open as a database?
The "Connection:=fName" bit is trying to connect
to a database, which a text file is not.

Do you just need to open the file if so try,
Workbooks.OpenText Filename:=fileToOpen...) There are some
more fields you can fill in here, but that is the basics.


  #3   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

Here's an update. I had a senior moment, and was using the wrong set of parameters, which got me past my original issue. I was originally having trouble connecting to my fName. Now, I am throwing an error in the ".Refresh BackgroundQuery:=False" statement. I tried changing it to "True" but that did not work either. Any ideas anyone? Thank you. Here is a snippet

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

With ActiveSheet.QueryTables.Add(Connection:="TEXT; C:\Macros\Datafiles\" & fName, Destination:=Range("A1")
.Name =
"TBJ20151270
.FieldNames = Tru
.RowNumbers = Fals
.FillAdjacentFormulas = Fals
.PreserveFormatting = Tru
.RefreshOnFileOpen = Fals
.RefreshStyle = xlInsertDeleteCell
.SavePassword = Fals
.SaveData = Tru
.AdjustColumnWidth = Tru
.RefreshPeriod =
.TextFilePromptOnRefresh = Fals
.TextFilePlatform = xlWindow
.TextFileStartRow =
.TextFileParseType = xlDelimite
.TextFileTextQualifier = xlTextQualifierDoubleQuot
.TextFileConsecutiveDelimiter = Fals
.TextFileTabDelimiter = Fals
.TextFileSemicolonDelimiter = Fals
.TextFileCommaDelimiter = Fals
.TextFileSpaceDelimiter = Fals
.TextFileOtherDelimiter = ":
.TextFileColumnDataTypes = Array(1, 1, 1
.Refresh BackgroundQuery:=Fals

End Wit
End I
End Sub
  #4   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default Import text file without specifying a path macro won't work

You should already have the path stored in fName, you
don't need to add C:\Macros\DataFiles again. Try just
using fName

-----Original Message-----
Here's an update. I had a senior moment, and was using

the wrong set of parameters, which got me past my original
issue. I was originally having trouble connecting to my
fName. Now, I am throwing an error in the ".Refresh
BackgroundQuery:=False" statement. I tried changing it
to "True" but that did not work either. Any ideas anyone?
Thank you. Here is a snippet.

Sub GetFile()
Dim fName As String
fName = Application.GetOpenFilename(FileFilter:="Text

Files (*.txt),*.txt")
If fName < "False" Then

With ActiveSheet.QueryTables.Add(Connection:="TEXT;

C:\Macros\Datafiles\" & fName, Destination:=Range("A1"))
.Name = _
"TBJ20151270"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier =

xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = ":"
.TextFileColumnDataTypes = Array(1, 1, 1)
.Refresh BackgroundQuery:=False

End With
End If
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
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 05:02 PM.

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"