我希望宏能够从网格中获取网址中的 sc_id 参数' [Data Download.xlsx] DATA'!$ B $ 1。 你能帮我修改一下代码吗?
Private Const NUMBER_OF_PAGES As Byte = 8
Sub DataDownload()
Dim URL_TEMPLATE As String
URL_TEMPLATE = "URL;http://www.moneycontrol.com/stocks/hist_stock_result.php?sc_id=" _
& ws.Range("B1").Value2 & "&pno={0}&hdn=daily&fdt=2000-01-01&todt=2015-12-31"
Dim page As Byte
Dim queryTableObject As QueryTable
Dim url As String
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("DATA")
For page = 1 To NUMBER_OF_PAGES
url = VBA.Strings.Replace(URL_TEMPLATE, "{0}", page)
Set queryTableObject = ws.QueryTables.Add(Connection:=url, _
Destination:=ws.Range("A:A").Find("*", , , , , xlPrevious).Offset(1, 0))
queryTableObject.FieldNames = True
queryTableObject.RowNumbers = False
queryTableObject.FillAdjacentFormulas = False
queryTableObject.PreserveFormatting = True
queryTableObject.RefreshOnFileOpen = True
queryTableObject.BackgroundQuery = True
queryTableObject.RefreshStyle = xlOverwriteCells
queryTableObject.SavePassword = False
queryTableObject.SaveData = False
queryTableObject.AdjustColumnWidth = False
queryTableObject.RefreshPeriod = 0
queryTableObject.WebSelectionType = xlSpecifiedTables
queryTableObject.WebFormatting = xlWebFormattingNone
queryTableObject.WebTables = "4"
queryTableObject.WebPreFormattedTextToColumns = True
queryTableObject.WebConsecutiveDelimitersAsOne = True
queryTableObject.WebSingleBlockTextImport = True
queryTableObject.WebDisableDateRecognition = True
queryTableObject.WebDisableRedirections = True
queryTableObject.Refresh BackgroundQuery:=False
Next page
End Sub
答案 0 :(得分:0)
如果您在同一工作表中获得了QueryTables
添加的值,则可以尝试在循环之前添加此行。
<强> EDIT2:强>
删除此行:
Private Const URL_TEMPLATE As String = "URL;http://www.moneycontrol...."
在你的sub:
中将URL_TEMPLATE
声明为变量
Dim URL_TEMPLATE As String
URL_TEMPLATE = "URL;http://www.moneycontrol.com/stocks/hist_stock_result.php?sc_id=" _
& ws.Range("B1").Value2 & "&pno={0}&hdn=daily&fdt=2000-01-01&todt=2015-12-31"
然后在循环内部,使用原始代码设置url
。
url = VBA.Strings.Replace(URL_TEMPLATE, "{0}", page)
我希望这次能够指出它。