I'm fairly new to VB and have been having trouble trying to get the QueryTable connection to actually populate the data into the active worksheet in Excel. It works in that it promts me to navigate and select my data source file. The problem is once I select the data file source I get a "Run-Time error 1004" with the message that MS Excel cannot access the file "insert file path here" that I had just selected.
Can anyone help? Is something wrong with my code? The file is not in use when the application tries to access it.
Here's the code:
Code:Sub ImportAutoRead() Dim fopen As Variant fopen = Application.GetOpenFilename("Auto Read Files (*.imp), *.imp") With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;" & fopen _ , Destination:=Range("$A$2")) .Name = fopen .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 = xlFixedWidth .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(9, 2, 2, 2, 9) .TextFileFixedColumnWidths = Array(106, 12, 4, 6) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub


Reply With Quote