简体   繁体   中英

Importing Data from CSV to XLSM

Following is the macro code for my application to get the data from a CSV file to an Excel file. It is located in the same location. But I am unable to import the data.

It is giving an error:

Runtime Error 1004 Method 'Range of object' _Global' failed.

Excel VBA based application code:

Sub LoadFromFile()
Dim fileName As String
Dim folder As String
Dim lastRow As Long
Dim dest As String


If IsEmpty(Range("A1").Value) = True Then
    lastRow = 1
Else: If IsEmpty(Range("A1").Value) = False Then GoTo Line1x
Line1x:
With ActiveSheet
        lastRow = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
    End With
        End If
ActiveSheet.UsedRange.Clear

dest = "$A$" & lastRow
folder = "C:\xampp\htdocs\sites\repairrequest\database.csv"

With ActiveSheet.QueryTables _
    .Add(Connection:="TEXT;" & folder, Destination:=Range("dest"))
    .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 = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With

End Sub

答案已在评论中解决

.Add(Connection:="TEXT;" & folder, Destination:=Range("$A$" & lastRow))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM