VBA从单元格进行Web查询
我是VBA的初学者,但我尝试使用Excel中的“从Web”工具从工作表中的指定单元格提取数据。下面我有两个不同的代码。第一个可以工作,但它在VBA从单元格进行Web查询,vba,excel,Vba,Excel,我是VBA的初学者,但我尝试使用Excel中的“从Web”工具从工作表中的指定单元格提取数据。下面我有两个不同的代码。第一个可以工作,但它在Source=Csv.Document(Web.Contents(“)中有实际链接ftp://ftp.hp.com/pub/softpaq/sp82501-83000/sp82564.cva“”)..行。运行此宏可以正常工作 但是,我想从单元格中获取链接,而不是手动输入链接,所以我在第二段代码中对其进行了一些更改。因此,我唯一真正更改的是,我创建了一个变量,
Source=Csv.Document(Web.Contents(“)中有实际链接ftp://ftp.hp.com/pub/softpaq/sp82501-83000/sp82564.cva“”)..
行。运行此宏可以正常工作
但是,我想从单元格中获取链接,而不是手动输入链接,所以我在第二段代码中对其进行了一些更改。因此,我唯一真正更改的是,我创建了一个变量,用于存储包含所需链接的单元格值,然后在Web.Contents()中使用该变量
。但是,这会引发一个错误
(运行时错误“1004”:应用程序定义的错误或对象定义的错误)
使用调试工具显示它在.Refresh BackgroundQuery:=False
行停止
我不确定问题是什么,因为我所做的只是使用同一个链接,但使用一个变量来保存链接,而不是实际的链接本身
Sub query()
ActiveWorkbook.Queries.Add Name:="sp85090", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(Web.Contents(""ftp://ftp.hp.com/pub/softpaq/sp82501-83000/sp82564.cva""),[Delimiter=""#(tab)"", Columns=1, Encoding=65001, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=sp85090;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [sp85090]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
' .ListObject.DisplayName = "_sp85090_2"
.Refresh BackgroundQuery:=False
End With
ActiveWorkbook.Queries("sp85090").Delete
ActiveWorkbook.Connections("Connection").Delete
Application.CommandBars("Queries and Connections").Visible = False
End Sub
Sub query()
softpaqLink = Sheets("test").Cells(3, "H").Value
ActiveWorkbook.Queries.Add Name:="sp85090", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(Web.Contents(""softpaqLink""),[Delimiter=""#(tab)"", Columns=1, Encoding=65001, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=sp85090;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [sp85090]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
' .ListObject.DisplayName = "_sp85090_2"
.Refresh BackgroundQuery:=False
End With
ActiveWorkbook.Queries("sp85090").Delete
ActiveWorkbook.Connections("Connection").Delete
Application.CommandBars("Queries and Connections").Visible = False
End Sub
- 确保名为
的工作表具有URLtest
- 始终将变量定义的softpaqLink声明为存储路径的字符串
应该是“softpaqLink”
”&softpaqLink&“
- 使用
而不是Thisworkbook
ActiveWorkbook
谢谢!但是,这仍然会导致相同的错误,并且不会将数据拉入。这太好了,非常感谢!为什么它必须是“&softpaqLink&”而不是“softpaqLink”?我猜,
”&softpaqLink&
的计算结果为ftp://ftp.hp.com/pub/softpaq/sp82501-83000/sp82564.cva"
和functionWeb.Contents(
需要路径作为字符串,因此需要附加引号。
Sub query1()
Dim softpaqLink As String
softpaqLink = Sheets("test").Cells(3, "H").Value
ThisWorkbook.Queries.Add Name:="sp85090", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(Web.Contents(""" & softpaqLink & """),[Delimiter=""#(tab)"", Columns=1, Encoding=65001, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ThisWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=sp85090;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [sp85090]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
' .ListObject.DisplayName = "_sp85090_2"
.Refresh BackgroundQuery:=False
End With
ThisWorkbook.Queries("sp85090").Delete
ThisWorkbook.Connections("Connection").Delete
Application.CommandBars("Queries and Connections").Visible = False
End Sub