1、第一步,获取股票代码,复制其中一部分到第一个工作表A4到A127,然后通过程序把每一个代码写入到不同的工作表A2位置,并对该工作表以该股票代码命名。程序如下:Sub 工作表命名()For i = 4 To 127Sheets(i).Range("a2") = "'" & Sheets(1).Range("a" & i)Next iFor i = 4 To Sheets.CountSheets(i).Name = Sheets(i).Range("a2").ValueNextEnd Sub
2、第二步,获取股票历史数据。代码如下:Private Function GetSource(sURL As String) As String Dim oXHTTP As Object Set oXHTTP = CreateObject("MSXML2.XMLHTTP") oXHTTP.Open "GET", sURL, False oXHTTP.Send GetSource = oXHTTP.responsetext Set oXHTTP = NothingEnd FunctionSub 历史数据() Dim objXML As Object Dim txtContent As String Dim i As Integer Dim strCode As String Dim gp As String Dim kaishihang Dim arr, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10, arr11 On Error Resume NextEndRow = Range("a65536").End(xlUp).RowstartRow = 4If startRow <= EndRow Then Range(Cells(startRow, 1), Cells(EndRow, 11)).Value = ""Else Exit SubEnd If Set objXML = CreateObject("Microsoft.XMLHTTP") gp = [A2] For h = 1 To 4 For m = 1 To 4 kaishihang = [A65535].End(xlUp).Row nian = Replace(Str(Year(Now) + 1 - h), " ", "") jidu = Replace(Str(4 + 1 - m), " ", "") With objXML .Open "GET", "http://quotes.money.163.com/trade/lsjyj_" + gp + ".html?year=" + nian + "&season=" + jidu + "", False .Send If objXML.Status = 200 Then txtContent = .responsetext arr = Split(txtContent, "'><td>") For i = 1 To UBound(arr) arr1 = Split(arr(i), "</td><td") Cells(i + kaishihang, 1) = Right(Left(arr1(0), 10), 10) arr2 = Split(arr1(1), Chr(60)) Cells(i + kaishihang, 2) = Mid(arr2(0), InStr(arr2(0), ">") + 1) arr3 = Split(arr1(2), Chr(60)) Cells(i + kaishihang, 3) = Mid(arr3(0), InStr(arr3(0), ">") + 1) arr4 = Split(arr1(3), Chr(60)) Cells(i + kaishihang, 4) = Mid(arr4(0), InStr(arr4(0), ">") + 1) arr5 = Split(arr1(4), Chr(60)) Cells(i + kaishihang, 5) = Mid(arr5(0), InStr(arr5(0), ">") + 1) arr6 = Split(arr1(5), Chr(60)) Cells(i + kaishihang, 6) = Mid(arr6(0), InStr(arr6(0), ">") + 1) arr7 = Split(arr1(6), Chr(60)) Cells(i + kaishihang, 7) = Mid(arr7(0), InStr(arr7(0), ">") + 1) arr8 = Split(arr1(7), Chr(60)) Cells(i + kaishihang, 8) = Mid(arr8(0), InStr(arr8(0), ">") + 1) arr9 = Split(arr1(8), Chr(60)) Cells(i + kaishihang, 9) = Mid(arr9(0), InStr(arr9(0), ">") + 1) arr10 = Split(arr1(9), Chr(60)) Cells(i + kaishihang, 10) = Mid(arr10(0), InStr(arr10(0), ">") + 1) arr11 = Split(arr1(10), Chr(60)) Cells(i + kaishihang, 11) = Mid(arr11(0), InStr(arr11(0), ">") + 1) Next i End If End With Next m Next hSet objXML = NothingEnd Sub
3、第三步,获取上证历史数据,并获取所有股票的历史数据。程序如下:Sub 所有股票历史数据获取()Application.ScreenUpdating = False Dim s As String, gp As String, nian As String, jidu As String, s1 As String Dim arr, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9 Dim i, h As Long Dim kaishihang Dim LastRow As Long, r As Long On Error Resume NextEndRow = Sheet2.Range("a65536").End(xlUp).RowstartRow = 4If startRow <= EndRow Then Sheet2.Range(Sheet2.Cells(startRow, 1), Sheet2.Cells(EndRow, 9)).Value = ""Else Exit SubEnd If For h = 1 To 5 For m = 1 To 4 kaishihang = Sheet2.[A65535].End(xlUp).Row nian = Replace(Str(Year(Now) + 1 - h), " ", "") jidu = Replace(Str(4 + 1 - m), " ", "") s1 = "http://quotes.money.163.com/trade/lsjysj_zhishu_000001.html?year=" + nian + "&season=" + jidu + "" s = GetSource(s1) arr = Split(s, "'><td>") For i = 1 To UBound(arr) arr1 = Split(arr(i), "</td><td") Sheet2.Cells(i + kaishihang, 1) = Right(Left(arr1(0), 4), 4) & "-" & Right(Left(arr1(0), 6), 2) & "-" & Right(Left(arr1(0), 10), 2) arr2 = Split(arr1(1), Chr(60)) Sheet2.Cells(i + kaishihang, 2) = Mid(arr2(0), InStr(arr2(0), ">") + 1) arr3 = Split(arr1(2), Chr(60)) Sheet2.Cells(i + kaishihang, 3) = Mid(arr3(0), InStr(arr3(0), ">") + 1) arr4 = Split(arr1(3), Chr(60)) Sheet2.Cells(i + kaishihang, 4) = Mid(arr4(0), InStr(arr4(0), ">") + 1) arr5 = Split(arr1(4), Chr(60)) Sheet2.Cells(i + kaishihang, 5) = Mid(arr5(0), InStr(arr5(0), ">") + 1) arr6 = Split(arr1(5), Chr(60)) Sheet2.Cells(i + kaishihang, 6) = Mid(arr6(0), InStr(arr6(0), ">") + 1) arr7 = Split(arr1(6), Chr(60)) Sheet2.Cells(i + kaishihang, 7) = Mid(arr7(0), InStr(arr7(0), ">") + 1) arr8 = Split(arr1(7), Chr(60)) Sheet2.Cells(i + kaishihang, 8) = Mid(arr8(0), InStr(arr8(0), ">") + 1) arr9 = Split(arr1(8), Chr(60)) Sheet2.Cells(i + kaishihang, 9) = Mid(arr9(0), InStr(arr9(0), ">") + 1) Next i Next mNext hApplication.ScreenUpdating = Truen = Worksheets.CountFor i = 4 To n Worksheets(i).Activate 历史数据NextEnd Sub
4、第四步,保存工作簿,并对该工作簿命名为1,然后复制该工作簿27份,把所有股票代码写入每一个工作簿第一个工作表的A3到A127列。然后根据第一步对工作簿内的工作表命名,并在每一个工作表A2单元格内写入股票代码。
5、第五步,把这些工作簿存放到炒股文件夹下的数据库文件下。然后在炒股文件夹下新建一个工作簿,命名为“一键更新表格”,写入如下代码:Sub 所有股票历史数据更新()Dim wb As WorkbookFor i = 1 To 27 Set wb = Workbooks.Open(ThisWorkbook.Path & "\数据库\" & i & ".xlsb") Application.Run "'" & wb.Path & "\" & i & ".xlsb'!所有股票历史数据获取"wb.Savewb.CloseNext iEnd sub当启动这一段代码以后,程序会自动一个一个的打开对应的工作簿然后获取里面所有股票的历史数据,存放在对应的工作表当中。
6、第六步,对相应的程序添加按钮,这样,我们在启动对应程序时,只需要点一下对应的按钮就可以实现了。这样,我们通过多个工作簿就实现了获取所有股票历史数据的方法。而后面,我们只需要通过获取对应股票每一天的历史数据,我们就能够在任意一天很快速的能够拥有所有股票的历史数据了。