DougTucker
Technical User
I need help on a "QueryTables" module that prompts the user for a .csv file name, then imports it into the current worksheet. The file will be ";" (semicolon) delimited. I've created the basic code using the RecordMacro feature, but I can't get it to work with the prompt for a file name. Please review the following and let me know where it's going wrong (I get an error "Application Defined or Object Defined Error" on the first line of the "With ActiveSheet" statement...
Thanks in advance for your help.
~ Doug T.
Thanks in advance for your help.
~ Doug T.
Code:
Sub TEST_ImportCSV()
'
' TEST_ImportCSV Macro
' Macro recorded 8/20/2009
'
Dim ImportedFile As String
'
ImportedFile = Application.GetOpenFilename _
("CSV (Comma delimited) (*.csv), *.csv", , _
"Open CSV File Exported from XYZ Application")
If ImportedFile = "" Then Exit Sub
'Insert new worksheet and name "ImportedFile"
With ActiveWorkbook.Sheets
.Add after:=Worksheets(Worksheets.Count)
.Name = "ImportedFile"
End With
'<<HERE'S WHERE THE ERROR OCCURS>>
With ActiveSheet.QueryTables.Add(Connection:=ImportedFile _
, Destination:=Range("A1"))
.Name = "NewWorksheet"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierNone
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = ";"
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Exit Sub
End Sub