Sub 下载商品现货与期货价格对比数据()
On Error Resume Next
Dim Url, HTML, r
Dim mr, n As Long, i%, j%, d
ActiveSheet.UsedRange.Offset(3).Clear
d = InputBox("请输入开始日期与终止日期", "日期范围", Format(Date, "yyyy-mm-dd/yyyy-mm-dd"))
Set HTML = CreateObject("htmlfile")
With CreateObject("msxml2.xmlhttp")
For n = Format(Split(d, "/")(0), "0") To Format(Split(d, "/")(1), "0")
mr = ActiveSheet.UsedRange.Rows.Count
If n > Format(Split(d, "/")(0), "0") Then Range("1:3").Copy Range("A" & mr + 1)
Range("b" & IIf(n > Format(Split(d, "/")(0), "0"), mr + 1, mr - 2)) = Format(n, "生意社:mm月dd日商品现货与期货价格对比表")
Url = "http://www.100ppi.com/sf/day-" & Format(n, "yyyy-mm-dd") & ".html"
.Open "get", Url, False
.send
HTML.body.innerhtml = .responsetext
Set r = HTML.all.tags("table")(1).Rows
For i = 2 To r.Length - 1
For j = 0 To r(1).Cells.Length
Cells(IIf(n > Format(Split(d, "/")(0), "0"), mr + 1, mr - 2) + i + 1, j + 1) = r(i).Cells(j).innertext
Cells(IIf(n > Format(Split(d, "/")(0), "0"), mr + 1, mr - 2) + i + 1, 5) = Replace(Replace(Split(Split(r(i).Cells(4).innerhtml, "color=")(1), "", ""), "red>", "")
Cells(IIf(n > Format(Split(d, "/")(0), "0"), mr + 1, mr - 2) + i + 1, 8) = Replace(Replace(Split(Split(r(i).Cells(7).innerhtml, "color=")(1), "", ""), "red>", "")
Next j
Next i
Next n
End With
End Sub