错误438 - ActiveWorkbook.queries

时间:2018-05-22 18:37:13

标签: excel vba excel-vba

我在Excel文件中有以下代码:

Sub PullData()
ActiveWorkbook.Queries.Add Name:="Sheet1", Formula:= _
    "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.Workbook(Web.Contents(""Link.xlsx""), null, true)," & Chr(13) & "" & Chr(10) & "    Sheet1_Sheet = Source{[Item=""Sheet1"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Sheet1_Sheet,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "" & _
    "    #""Changed Type"""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Sheet1;Extended Properties=""""" _
    , Destination:=Range("$B$3")).QueryTable
    .CommandType = xlCmdSql
    .CommandText = Array("SELECT * FROM [Sheet1]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "Sheet1"
    .Refresh BackgroundQuery:=False
End With End Sub


Sub ClearData() ClearData Macro

    Columns("B:C").Select
    Selection.ClearContents
     ActiveWorkbook.Queries("Sheet1").Delete 
End Sub

在我的电脑(以及我公司的其他几台电脑)上,这个文件没有任何问题。但是,在某些计算机上,我收到了

  

"运行时错误:' 438':对象不支持此属性或   方法"

在VBA中,它会突出显示行ActiveWorkbook.Queries("Sheet1").Delete

为了解释它应该如何工作,我有一个在线Excel文件,其中包含一个包含条目的列表。有2个按钮,第一个从该文件中提取数据,第二个按钮删除数据。我只使用一个按钮来刷新数据库,但由于某种原因,我一直收到与已经存在的查询相关的错误。

如果之前有人问过我,我很抱歉,但我刚开始与VBA合作,而且任何支持都受到欢迎。

1 个答案:

答案 0 :(得分:0)

显然Workbook个对象没有Queries个集合。您可能意味着ActiveWorkbook.Worksheets("Sheet1").Delete吗?