我尝试通过每次运行时更改URL来动态导入基于Web的CSV文件。所以我通过输入最后一个字符串作为用户通过输入框输入的变量来获得我动态管理的URL。
问题是,我得到了一个"运行时错误''对象不支持此方法。
所以这是正在传递的URL的一个例子。你可以改变" BRZ"到" ASB"如果你愿意,可以看一个不同的例子。
http://cf3.myplumbingshowroom.com/scheduledScripts/brandDataAPI.cfm?brandCode=BRZ
如您所见,该链接将带您直接进入该文件夹。
我不知道这意味着什么。我看到很多人都遇到了同样的问题,但是" .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
答案 0 :(得分:0)
如果您已经有一个这个名字的表,就会发生这种情况。特别是如果你有一个这个名字的桌子并且你删除了它,但它虽然不可见但它还没有消失。
首先循环所有列表对象并删除任何具有建议名称的列表对象,例如"你好" :
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