|
楼主 |
发表于 2024-1-25 22:31
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
给大家分享下 我最后研究摸索出的代码 有不合适的地方还请多多指教。
- Sub cjsj() '采集部分数据
- Application.ScreenUpdating = False
-
- ' 程序开始
- On Error Resume Next
- ' Dim xmlhttp As Object
- Dim baseUrl As String
- Dim lotCode As String
- Dim dateStr As String
- Dim i, re, j, lastRow As Long
- Dim arr
- With Sheet2
- n = n + 1
- ' 获取A列最后一个非空单元格的行号
- lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
-
- ' 计算A列中最大的数字
- j = Application.WorksheetFunction.Max(.Range("A1:A" & lastRow)) - 1
- End With
-
-
- 'lotCode = "10036"
- dateStr = Format(Date, "yyyy-mm-dd") ' 当天日期
-
-
- Set xmlhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
- 'url = "https://api.api68.com/CQShiCai/getBaseCQShiCaiList.do?lotCode=10036&date=2024-01-24"
- url = "https://api.api68.com/CQShiCai/getBaseCQShiCaiList.do?lotCode=10036&date=" & dateStr
-
- With xmlhttp
- .Open "Get", url, False
- .send
-
- ' 获取返回数据
- If .Status = 200 Then
- m = .responseText
- Else
-
- Exit Sub
-
- End If
- End With
-
- Set oDm = CreateObject("htmlfile")
- Set oW = oDm.parentWindow
- oW.execScript ("var query1 = " & m)
- l = oW.Eval("M=query1[""result""][""data""].length") - 1
-
- ReDim arr(1 To l + 1, 1 To 8)
-
- For i = 0 To j
-
- Dim drawData As Object
- Set drawData = oW.Eval("M=query1[""result""][""data""][" & i & "]")
-
- arr(i + 1, 1) = drawData.preDrawIssue
-
- arr(i + 1, 2) = drawData.preDrawTime
- arr(i + 1, 3) = drawData.preDrawCode
- arr(i + 1, 4) = Split(arr(i + 1, 3), ",")(0)
- arr(i + 1, 5) = Split(arr(i + 1, 3), ",")(1)
- arr(i + 1, 6) = Split(arr(i + 1, 3), ",")(2)
- arr(i + 1, 7) = Split(arr(i + 1, 3), ",")(3)
- arr(i + 1, 8) = Split(arr(i + 1, 3), ",")(4)
- Next
- ' 将数组中的数据写入表格
- With Sheet1
- re = .Range("a" & Rows.Count).End(xlUp).Row
- If re > 3 Then
- .Range("a3:H" & re).ClearContents
-
-
- End If
-
-
- .Range("A3").Resize(j + 1, 8).Value = arr
- End With
-
-
- Set oDom = Nothing
- Set oW = Nothing
- Application.ScreenUpdating = True
- Sheets("实时数据").[C1] = n
- End Sub
复制代码
|
|