求高手帮我处理一下EXCEL VBA 程序
上面图片数据的链接:view-source:http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?&r...
上面图片数据的链接:view-source:http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?&rand=random(10000)&symbol=sz002241&end_date=20130801&begin_date=20130601&type=plain
通过VBA 程序遍历网页数据,在获取数据时程序是从1开始往后获取的,本人想从41(上面只有41项)开始往前获取数据,所以请教公式应该怎么修改,程序如下
'Disigned By ExcelHome Yier_fang
Sub cnft_click()
Dim arrAllData, arrb
Dim rng As Range, i%
Application.ScreenUpdating = False
On Error Resume Next
For Each rng In Range("A22", [A65536].End(4))
arrAllData = GetStockAllData(rng.Value)
For i = 0 To UBound(arrAllData)
rng(1, 4 + i) = arrAllData(i, 1)
Next i
On Error GoTo 0
Next
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 Then
StockCode = "sh" & StockCode
ElseIf Left(StockCode, 2) = 0 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=20141017"
'开始读取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
当把
For i = 0 To nCntChd
改为
For i = objNode.ChildNodes.Length - 10 To objNode.ChildNodes.Length - 1
可以实现从后面开始往前获取数据,但每行数据记录开始的位置就不会在同一列上,变的很乱 展开
通过VBA 程序遍历网页数据,在获取数据时程序是从1开始往后获取的,本人想从41(上面只有41项)开始往前获取数据,所以请教公式应该怎么修改,程序如下
'Disigned By ExcelHome Yier_fang
Sub cnft_click()
Dim arrAllData, arrb
Dim rng As Range, i%
Application.ScreenUpdating = False
On Error Resume Next
For Each rng In Range("A22", [A65536].End(4))
arrAllData = GetStockAllData(rng.Value)
For i = 0 To UBound(arrAllData)
rng(1, 4 + i) = arrAllData(i, 1)
Next i
On Error GoTo 0
Next
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 Then
StockCode = "sh" & StockCode
ElseIf Left(StockCode, 2) = 0 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=20141017"
'开始读取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
当把
For i = 0 To nCntChd
改为
For i = objNode.ChildNodes.Length - 10 To objNode.ChildNodes.Length - 1
可以实现从后面开始往前获取数据,但每行数据记录开始的位置就不会在同一列上,变的很乱 展开
2个回答
2014-10-27 · 知道合伙人软件行家
关注
展开全部
Sub test()
r = Range("a65536").End(xlUp).Row
a = Range("a2:d" & r).Value
Sheets.Add
x = 2
For r = 1 To UBound(a)
b = Split(a(r, 4), "[")
n = UBound(b)
For i = 1 To 3
Cells(x, i).Resize(n, 1) = a(r, i)
Next
For i = 1 To n
Cells(x, 4) = "[" & b(i)
x = x + 1
Next
Next
End Sub
r = Range("a65536").End(xlUp).Row
a = Range("a2:d" & r).Value
Sheets.Add
x = 2
For r = 1 To UBound(a)
b = Split(a(r, 4), "[")
n = UBound(b)
For i = 1 To 3
Cells(x, i).Resize(n, 1) = a(r, i)
Next
For i = 1 To n
Cells(x, 4) = "[" & b(i)
x = x + 1
Next
Next
End Sub
追问
这个好像运行不来哦
当把
For i = 0 To nCntChd
改为
For i = objNode.ChildNodes.Length - 5 To objNode.ChildNodes.Length - 1
可以实现从后面开始往前获取数据,但每行数据记录开始的位置就不会在同一列上,就是每行第一个数据位置不会一样,很乱
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询