以後再補充這是幹嘛的,現在只是先整理下代碼
Sub cnft_click()
Dim arrAllData, arrb
Range("A:F").ClearContents
arrAllData = GetStockAllData(Range("H4"))
Application.ScreenUpdating = False
On Error Resume Next
arrb = Array("日期", "開盤價", "最高價", "成交價", "最低價", "成交量")
Dim i As Long
For i = 0 To 5
Range("A1").Offset(0, i).Value = arrb(i)
Next i
For i = 0 To UBound(arrAllData)
For j = 0 To 5
Range("a2").Offset(i, j).Value = arrAllData(i, j)
Next j
Next i
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
Function GetStockAllData(ByRef StockCode As String)
On Error Resume Next
If Len(StockCode) <> 6 Then Exit Function
'判斷上證還是深證
If Left(StockCode, 2) = 60 Or Left(StockCode, 2) = 58 Then
StockCode = "sh" & StockCode
ElseIf Left(StockCode, 2) = 0 Or Left(StockCode, 2) = 3 Then
StockCode = "sz" & StockCode
Else
StockCode = "sh600000"
End If
Dim iData As String
iData = Format(Month(Date), "00")
iData = iData & Format(Day(Date), "00")
iData = Year(Date) & iData
StockCode = "http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?symbol=" & StockCode
StockCode = StockCode & Chr(38) & "end_date=" & iData
StockCode = StockCode & Chr(38) & "begin_date=20080101"
'開始讀取XML內容
Dim XML, objNode, objAtr As Object
Dim nCntChd, nCntAtr As Long
Set XML = CreateObject("Microsoft.XMLDOM")
With XML
.async = False
.Load (StockCode)
End With
Set objNode = XML.documentElement
nCntChd = objNode.ChildNodes.Length - 1 'XML的記錄個數
Dim arrA
ReDim arrA(0 To nCntChd, 0 To 6)
'開始遍歷
For i = 0 To nCntChd
Set objAtr = objNode.ChildNodes.Item(i)
nCntAtr = objAtr.Attributes.Length - 1
For j = 0 To nCntAtr '遍歷一條記錄裏面的所有的記錄項,記錄是從0開始的
arrA(i, j) = objAtr.Attributes.Item(j).Text
Next j
Next i
Set objAtr = Nothing
Set objNode = Nothing
Set XML = Nothing
If Err.Number > 0 Then MsgBox ("查不到股票信息")
Err.Clear
On Error GoTo 0
GetStockAllData = arrA
End Function