马晓翔MMC
2024-12-04 04:28:38
最佳回答
1第一步,个股代码存放在工作表a2单元格,获取当日个股数据代码:function downloadhtm(url) on error resume next downloadhtm = true dim retrieval set retrieval = createobject("microsoft.xmlhttp") with retrieval .open "get", url, false, "", "" .send if .readystate <> 4 then downloadhtm = false exit function end if downloadhtm = strconv(.responsebody, vbunicode) end with set retrieval = nothingend functionsub 当日个股()dim th**code as stringdim datas() as string, url as stringdim i, j, k as integerdim s as stringon error resume nextapplication.screenupdating = falseif ([a4] = "" and [a5] = "") or [a4] <> date then th**code = getfullcode(cells(2, 1)) url = "http://qt.gtimg.cn/q=" & th**code '取得url后再获取数据,再分割 s = downloadhtm(url) '分割代码 datas = split(s, "~") if datas(5) > 0 then range("a4:k4").select selection.insert shift:=xldown cells(4, 1) = date cells(2, 2) = datas(1) cells(4, 5) = datas(3) '当前 cells(4, 6) = datas(31) '涨跌额(元) cells(4, 7) = datas(32) '涨跌幅(%) cells(4, 2) = datas(5) '开盘 cells(4, 3) = datas(33) '最高 cells(4, 4) = datas(34) '最低 cells(4, 10) = datas(43) '振幅(%) cells(4, 8) = datas(36) '成交量(万手) cells(4, 9) = datas(37) '成交额(亿) cells(4, 11) = datas(38) '换手率 end if end if end sub2第二步,获取大盘当日数据,以及所有股票当日数据。股票代码如下:sub 一键更新当日数据()dim th**code as stringdim datas() as string, url as stringdim i, j, k as integerdim s as stringon error resume nextapplication.screenupdating = falseif (sheet2.[a4] = "" and sheet2.[a5] = "") or sheet2.[a4] <> date then url = "http://hq.sinajs.cn/l**t=sh000001" '取得url后再获取数据,再分割 s = downloadhtm(url) '分割代码 datas = split(s, ",") if datas(30) <> sheet2.cells(4, 1) then sheet2.activateactivesheet.range(cells(4, 1), cells(4, 9)).select selection.insert shift:=xldown sheet2.cells(4, 1) = datas(30) sheet2.cells(4, 5) = datas(3) '当前 sheet2.cells(4, 6) = datas(3) - sheet2.cells(5, 5) '涨跌额(元) sheet2.cells(4, 7) = sheet2.cells(4, 6) * 100 / sheet2.cells(5, 5) '涨跌幅(%) sheet2.cells(4, 2) = datas(1) '开盘 sheet2.cells(4, 3) = datas(4) '最高 sheet2.cells(4, 4) = datas(5) '最低 sheet2.cells(4, 8) = datas(8) '成交量(万手) sheet2.cells(4, 9) = datas(9) '成交额(亿) end if end ifn = worksheets.countfor i = 4 to n worksheets(i).activate 当日个股nextsheets("汇总").activateapplication.screenupdating = trueend sub2本页面未经授权抓取自百度经验3第三步,在数据库下所有工作簿当中,写入这段程序。或者复制该工作簿27份,写入不同股票代码。4第四步,在炒股目录下新建工作簿“一键更新表格”,里面输入以下代码:sub 当日数据() dim wb as workbookapplication.calculation = xlcalculationmanual for i = 1 to 27 set wb = workbooks.open(th**workbook.path & "\数据库\" & i & ".xl**") application.run "'" & wb.path & "\" & i & ".xl**'!一键更新当日数据"wb.s**ewb.close next iapplication.screenupdating = true end sub这样,我们就实现了通过这段程序更新所有股票当日数据。5第五步,对应程序添加按钮,这样,我们在实现某一段程序功能的时候,就能够通过点击一个按钮即可实现对应的功能了。6第六步,如果只是更新当日所有股票数据到一个工作表,这个速度是很快的,大概只要3秒左右。但是考虑要对这些数据做分析,所以更新的数据要一段一段的写入到对应的工作表当中,并且,程序还要不断的执行打开-写入-保存-关闭等操作。所以整个过程大概要十几分钟。当然,通过这样的方式,我们就可以对所有的股票作单独分析,也可以做整体分析。end 20210311