PDA

View Full Version : Importing a .txt file based on file title that is input by user



flyfisher15
11-11-2011, 09:18 AM
I'm trying to have a .txt file imported to excel. The file to import would be selected based on user input.

Here's what I have. Whenever I run this i get an object error.



Sub FDR_Data_get()


Dim filedate As String



Sheets("FDR Data").Select

filedate = InputBox("Enter the FDR file load date in mm-dd-yyyy format", "Date Entry")

With Sheets("FDR Data").QueryTables.Add(Connection:= _
"TEXT;C:\Documents and Settings\####\Desktop\Macro File\Inbox\FDR PDF Report\*" & filedate & "*" _
, Destination:=Range("A1"))
.Name = "*" & filedate & "*"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False

End With

End Sub


Any help would be greatly appreciated!

Kenneth Hobs
11-11-2011, 07:42 PM
You have to give that function a file name with no wildcards. Might it not be better to use a file dialog to let them pick the file?

flyfisher15
11-14-2011, 09:53 AM
I haven't used a file dialog before. How do i go about setting that up?

Kenneth Hobs
11-18-2011, 12:07 PM
Function FileOpen(initialFilename As String, _
Optional sDesc As String = "Excel (*.xls)", _
Optional sFilter As String = "*.xls") As String
With Application.FileDialog(msoFileDialogOpen)
.ButtonName = "&Open"
.initialFilename = initialFilename
.Filters.Clear
.Filters.Add sDesc, sFilter, 1
.Title = "Date Entry"
.AllowMultiSelect = False
If .Show = -1 Then FileOpen = .SelectedItems(1)
End With
End Function

Sub FDR_Data_get()
Dim filedate As String
'Sheets("FDR Data").Select

filedate = FileOpen("C:\Documents and Settings\####\Desktop\Macro File\Inbox\FDR PDF Report\")

With Sheets("FDR Data").QueryTables.Add(Connection:= _
"TEXT;" & filedate & _
", Destination:=" & Sheets("FDR Data").Range("A1").Value)
.Name = "*" & filedate & "*"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub