Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Import Problem

Status
Not open for further replies.

EliseFreedman

Programmer
Dec 6, 2002
470
GB
Hi There

I am attempting to import a file into an existing workbook using the code below. I have used similar code in another workbook without any problems but this time round I am getting the following error. Does anyone know what is causing this?

Code:
The destination range is not on the same worksheet that the query table is being created on

The code that I am using is below

Code:
Private Sub CommandButton2_Click()


'
    Sheets("RFV").Select
    ActiveSheet.Rows("1:3").Select
    Selection.ClearContents
    ActiveSheet.Range("A1").Select
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;H:\RFV.csv", Destination _
        :=Range("A1"))
        .Name = "RFV"
        .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 = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    Columns("A:A").ColumnWidth = 18.29
End Sub
 



Hi Elsie,

Explicitly reference the destination range...
Code:
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;H:\RFV.csv", Destination _
        :=ActiveSheet.Range("A1"))
...

Skip,

[glasses] [red][/red]
[tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top