将数据从CSV导入XLSM

时间:2018-03-28 16:37:47

标签: excel vba excel-vba

以下是我的应用程序的宏代码,用于将数据从CSV文件获取到Excel文件。它位于同一个位置。但是我无法导入数据。

发出错误:

  

运行时错误1004方法'对象范围' _Global'失败。

基于Excel VBA的应用程序代码:

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

1 个答案:

答案 0 :(得分:2)

答案在评论中解决了

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