如何拒绝QueryTables URL的参数提示?
How to deny parameter prompt for QueryTables URL?
以下代码下载一个 .csv 文件并通过 QueryTables 将其导入 sheet。不幸的是,URL 包含“%5B%5D”,这让 Excel 认为缺少参数(由于 URL-Encoding)并迫使我输入。我已经尝试在百分比代码之间放置一些东西,但是 URL 不起作用。有没有办法避免提示?
Option Explicit
Dim URL_to_Pull As String
Dim ws As Worksheet
Sub Pull_Quality()
Application.Calculation = xlManual
URL_to_Pull = "https://this-is-a-link.com/download/csv?par%5B%5D=Hello&report_range=" & Sheets("base").Range("F_Year") & "-" & Sheets("base").Range("F_Month") & "-" & Sheets("base").Range("F_Day") & "+-+" & Sheets("base").Range("T_Year") & "-" & Sheets("base").Range("T_Month") & "-" & Sheets("base").Range("T_Day")
Set ws = Sheets("raw_data")
ws.Range("A:Z").ClearContents
Debug.Print URL_to_Pull
Call Connection_Create(URL_to_Pull, ws)
Application.Calculation = xlAutomatic
End Sub
Sub Connection_Create(Pull_URL As String, Input_Sheet As Worksheet)
With Input_Sheet.QueryTables.Add(Connection:="URL;" & Pull_URL, Destination:=Input_Sheet.Cells(1, 1))
.Name = "Query_Table"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Input_Sheet.Cells(1, 1).QueryTable.Delete
Input_Sheet.Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False
End Sub
parameter prompt window
https://www.microsoft.com/en-us/microsoft-365/blog/2009/07/31/using-parameters-with-web-queries/ 展示了如何处理网络查询中的参数。如果不需要提供值,大概可以将值设置为空字符串。
Sub Demo()
Dim oSh As Worksheet
Set oSh = ActiveSheet
With oSh.QueryTables.Add( _
"URL;http://www.jkp-ads.com/Articles/[""PageName""].htm", oSh.Range("A1"))
.BackgroundQuery = False
MsgBox .Parameters.Count
With .Parameters(1) '<<<<<<<<<<<<<<<<<<<<
.SetParam xlRange, oSh.Range("H11")
.RefreshOnChange = True
End With
End With
End Sub
以下代码下载一个 .csv 文件并通过 QueryTables 将其导入 sheet。不幸的是,URL 包含“%5B%5D”,这让 Excel 认为缺少参数(由于 URL-Encoding)并迫使我输入。我已经尝试在百分比代码之间放置一些东西,但是 URL 不起作用。有没有办法避免提示?
Option Explicit
Dim URL_to_Pull As String
Dim ws As Worksheet
Sub Pull_Quality()
Application.Calculation = xlManual
URL_to_Pull = "https://this-is-a-link.com/download/csv?par%5B%5D=Hello&report_range=" & Sheets("base").Range("F_Year") & "-" & Sheets("base").Range("F_Month") & "-" & Sheets("base").Range("F_Day") & "+-+" & Sheets("base").Range("T_Year") & "-" & Sheets("base").Range("T_Month") & "-" & Sheets("base").Range("T_Day")
Set ws = Sheets("raw_data")
ws.Range("A:Z").ClearContents
Debug.Print URL_to_Pull
Call Connection_Create(URL_to_Pull, ws)
Application.Calculation = xlAutomatic
End Sub
Sub Connection_Create(Pull_URL As String, Input_Sheet As Worksheet)
With Input_Sheet.QueryTables.Add(Connection:="URL;" & Pull_URL, Destination:=Input_Sheet.Cells(1, 1))
.Name = "Query_Table"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Input_Sheet.Cells(1, 1).QueryTable.Delete
Input_Sheet.Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False
End Sub
parameter prompt window
https://www.microsoft.com/en-us/microsoft-365/blog/2009/07/31/using-parameters-with-web-queries/ 展示了如何处理网络查询中的参数。如果不需要提供值,大概可以将值设置为空字符串。
Sub Demo()
Dim oSh As Worksheet
Set oSh = ActiveSheet
With oSh.QueryTables.Add( _
"URL;http://www.jkp-ads.com/Articles/[""PageName""].htm", oSh.Range("A1"))
.BackgroundQuery = False
MsgBox .Parameters.Count
With .Parameters(1) '<<<<<<<<<<<<<<<<<<<<
.SetParam xlRange, oSh.Range("H11")
.RefreshOnChange = True
End With
End With
End Sub