0

我在以下代码中遇到运行时错误。我对在 excel VBA 中使用查询有点陌生,我似乎无法找到错误所在。我意识到查询在“.Refresh BackgroundQuery:=False”行上执行,并且错误不一定来自该行。为了找出问题所在,我停止了代码并打开了我试图加载的查询,它看起来是正确的。它抓取了我请求的数据,但由于某种原因,代码无法完成。这可能是查询字段的数据类型不匹配吗?

运行时错误 1004,应用程序定义或对象定义错误

对于间隔 = LBound(AssociateList) 到 UBound(AssociateList)

ActiveWorkbook.Queries.Add Name:=AssociateList(Interval), Formula:= _
    "let" & Chr(13) & Chr(10) & "Source = Excel.Workbook(File.Contents(""C:\Users\...\Desktop\" & AssociateList(Interval) & " " & Month & ".xlsx""), null, true)," & Chr(13) & "" & Chr(10) & "All_Table = Source{[Item=""All"",Kind=""Table""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(All_Table,{{""LOAD_DATE"", type datetime}, {""CLNAME_S"", type text}, {""MEDIA" & _
    """, type text}, {""MOS"", type date}, {""TOTAL_INVOICED"", type number}, {""TOTAL_ORDERED"", type number}, {""TOTAL_CLEARED"", type number}, {""TOTAL_UNCLEARED"", type number}, {""TOTAL_BILLED_NET"", type number}, {""TOTAL_BILLABLE_NET"", type number}, {""OFFICE"", type text}, {""CLIENTCODE"", type text}, {""Key"", Int64.Type}, {""Standard Comment"", type text}, {""" & _
    "Additional Notes"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = AssociateList(Interval)
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=All", _
    Destination:=Range("$A$1")).QueryTable
    .CommandType = xlCmdSql
    .CommandText = Array("SELECT * FROM [All]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "Associate" & Interval
    .Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
4

0 回答 0