VBA:运行时错误';438';:对象没有';不支持此属性或方法

VBA:运行时错误';438';:对象没有';不支持此属性或方法,vba,excel,Vba,Excel,我试图通过每次运行URL来动态导入基于web的CSV文件。因此,我得到了一个URL,通过输入用户通过输入框输入的最后一个字符串作为变量来动态管理它 问题是,我得到了一个“运行时错误'438”:对象不支持此方法 这是一个URL的示例,如果您愿意,可以将“BRZ”更改为“ASB”以查看不同的示例 如您所见,该链接将直接指向该文件夹 我不知道这意味着什么。我看到很多人都有同样的问题,但是“.ListObject.DisplayName”是返回错误的代码部分。我将代码部分下划线并突出显示,以便于参考

我试图通过每次运行URL来动态导入基于web的CSV文件。因此,我得到了一个URL,通过输入用户通过输入框输入的最后一个字符串作为变量来动态管理它

问题是,我得到了一个“运行时错误'438”:对象不支持此方法

这是一个URL的示例,如果您愿意,可以将“BRZ”更改为“ASB”以查看不同的示例

如您所见,该链接将直接指向该文件夹

我不知道这意味着什么。我看到很多人都有同样的问题,但是“.ListObject.DisplayName”是返回错误的代码部分。我将代码部分下划线并突出显示,以便于参考

Sub Macro3()
'Defining Variables
Dim myValue As Variant
Dim Link As Variant


'Creating a link
Link = "http://cf3.myplumbingshowroom.com/scheduledScripts/brandDataAPI.cfm?brandCode="


'creating a the variable to be passed into the link
myValue = InputBox("Enter your brand code here")


'completing our link
Link = "http://cf3.myplumbingshowroom.com/scheduledScripts/brandDataAPI.cfm?brandCode=" & myValue
'checking if it exists
MsgBox Link


'if the query already exists, we're going to delete it. Otherwise, we're going to ignore that function.


On Error Resume Next
ActiveWorkbook.Queries("brandDataAPI").Delete
On Error GoTo 0




    Application.CutCopyMode = False
    ActiveWorkbook.Queries.Add Name:="brandDataAPI", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Csv.Document(Web.Contents(" & Link & "),[Delimiter="","", Columns=11, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Header" & _
        "s"",{{""BrandName"", type text}, {"" BrandCode"", type text}, {"" BrandID"", Int64.Type}, {"" datalastUpdate"", type date}, {"" numproducts"", Int64.Type}, {""priceMethod"", type text}, {""URL"", type text}, {""showPrice"", Int64.Type}, {""MAP_YN"", Int64.Type}, {""MAP"", type text}, {""msrpNotes"", 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=""brandDataAPI"";Extended Properties=""""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [brandDataAPI]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = False
        .Refresh BackgroundQuery:=True
        ***.ListObject.DisplayName***
    End With
End Sub

如果您已经有一个同名的表,就会发生这种情况。特别是如果您有一个同名的表,并且您删除了它,但它并没有完全消失,尽管它不可见

因此,首先循环所有ListObject并删除任何具有建议名称的对象,例如“Hello”:


星号是您代码的一部分,还是试图强调?另外,可能尝试从ActiveSheet.ListObjects.Add中删除ActiveSheet。即使我将VBA代码放入新的工作表中,也会发生此错误。您是否可以在创建后以表为目标,而不是在创建时以查询表为目标?似乎您还可以通过名称框ch很奇怪。很抱歉,这帮了我很大的忙,因为这为我解决了一个类似的问题。
Option Explicit

Sub test()
    Dim ws As Worksheet
    Dim l As ListObject
    For Each ws In ThisWorkbook.Worksheets
        For Each l In ws.ListObjects
            If l.Name = "Hello" Then l.Delete
        Next l
    Next ws

'Do your stuff
End Sub