關閉廣告

(不定期更新)使用VBA解決 excel web 查詢無法匯入、匯入太慢的股市資料

謝謝您分享給我的表格
早上臨時用您的表格改的

試看看裡面的3個按鈕

盤中也可以用,不過因為台灣50、中型100資料來源是yahoo,會lag

其它eps、股利、年線..等等
因為我不知道來源網址,所以暫時沒加上更新功能

ans:感謝你分享的程式~一般eps我都到goodinfo抓,它可以有表格式的整理,且只要動到股票代碼就能找到該股票的eps,只是累積、個季,年 eps,goodinfo是用一個下拉式選單,不是一個明確的網址,

https://goodinfo.tw/StockInfo/StockFinDetail.asp?RPT_CAT=IS_M_QUAR_ACC&STOCK_ID=2317

goodinfo網址

至於三年線,十年線均價是類似下面的格式,一般似乎也不好抓的感覺~

https://goodinfo.tw/StockInfo/ShowK_Chart.asp?STOCK_ID=2317&CHT_CAT2=MONTH

三年線十年線均價

下面網址是淨值變化
https://goodinfo.tw/StockInfo/StockFinDetail.asp?RPT_CAT=BS_M_QUAR&STOCK_ID=2317

淨值變化

以上是我平常在查這些資料的網址,excel似乎不一定能抓所有網址的資料,不一定能抓,但平常當查資料的網站應該是還不錯用~
Acer_kewei wrote:
以上是我平常在查這些資料的網址,excel似乎不一定能抓所有網址的資料...(恕刪)


這些網址是我在2017年寫的範例,全部都可以抓
剛剛檢查一下,雖然會出現一些bug,但程式還可以用

網站不知道改版幾次,標題、名稱都不一樣了,明天有空我再修正程式碼
有興趣可以先抓舊版來玩玩=>149樓、294樓
突然發現goodinfo 台灣股市資訊網,改版很久了
試了一下2017年寫的範例,因網站改版,出了一大堆bug
重新修正一下程式碼,請參考

注意:
程式中沒有加入除錯功能
當出現網站維護、網路中斷…等等,無法下載資料的狀況時,程式碼會中斷
請參考此樓其它範例自行加入除錯功能


20190329更新程式碼 (加入標題、股利政策、排版一下財務比率表)



'=========================================
'程式碼放在thisworkbook
Private Sub Workbook_Open()
Sheets("acc").Cells.Clear
Sheets("股價漲跌資料表").Cells.Clear
Sheets("股利政策").Cells.Clear
Call get_Q_Y
Call addlistbox
End Sub
'=========================================
'程式碼放到模組
Public Q(), Y()

Sub get_Q_Y() '季別、年度


Dim HTMLsourcecode As Object, Getxml As Object, Get_Q, Get_Y, i As Integer, j As Integer
Set HTMLsourcecode = CreateObject("htmlfile")
Set Getxml = CreateObject("WinHttp.WinHttpRequest.5.1")

url = "https://goodinfo.tw/StockInfo/StockFinDetail.asp?RPT_CAT=BS_M_QUAR&STOCK_ID=2412"
url_a = "https://goodinfo.tw/StockInfo/StockFinDetail.asp?STEP=DATA&STOCK_ID=2412&RPT_CAT=BS_M_YEAR&QRY_TIME=20184"

For i = 1 To 2

With Getxml
.Open "POST", choose(i, url, url_a), False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
If i = 2 Then .setRequestHeader "Referer", url
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.Send

HTMLsourcecode.body.innerhtml = convertraw(.ResponseBody)
If InStr(HTMLsourcecode.body.innertext, "查 無 資 料 !!") > 0 Then
MsgBox "查無資料"
Exit Sub
End If

If i = 1 Then
Set Get_Q = HTMLsourcecode.getelementbyid("QRY_TIME")
Debug.Print Get_Q.Length

ReDim Q(Get_Q.Length - 1)

For j = 0 To Get_Q.Length - 1
Q(j) = Get_Q(j).innertext
Next j

Else
Set Get_Y = HTMLsourcecode.getelementbyid("QRY_TIME")
Debug.Print Get_Y.Length

ReDim Y(Get_Y.Length - 1)


For j = 0 To Get_Y.Length - 1
Y(j) = Left(Get_Y(j).innertext, 4)
Next j
End If

End With

Next i



Set HTMLsourcecode = Nothing
Set Getxml = Nothing
Set Get_Q = Nothing
Set Get_Y = Nothing


End Sub



Sub addlistbox()


Dim list_0, list_1, list_2, list_3, download, download_1, download_2, download_3


With Sheets("Goodinfo")
.Select
.Cells.Clear
.DrawingObjects.Delete


' .ListBoxes("list_0").Delete
' .ListBoxes("list_1").Delete
' .ListBoxes("list_2").Delete
' .Buttons("download").Delete

.Columns.ColumnWidth = 10

Set list_0 = .ListBoxes.Add(.Range("b7").Left + 5, .Range("b7").Top, 100, 100)
With list_0
.Name = "list_0"
list_0.List = listdata(0)
.Selected(1) = True
.OnAction = "list_0_change"

End With

Set list_1 = .ListBoxes.Add(.Range("d7").Left + 5, Range("d7").Top, 100, 100)
With list_1
.Name = "list_1"
list_1.List = listdata(1)
.Selected(1) = True
.OnAction = "list_1_change"
End With

Set list_2 = .ListBoxes.Add(.Range("f7").Left + 5, Range("f7").Top, 100, 100)
With list_2
.Name = "list_2"
list_2.List = listdata(6)
.Selected(1) = True
.OnAction = "list_2_change"
End With

Set list_3 = .ListBoxes.Add(.Range("h7").Left + 5, Range("h7").Top, 100, 100)
With list_3
.Name = "list_3"
list_3.List = Array("三個月" & Space(30) & ",90", "六個月" & Space(30) & ",180", "一年" & Space(30) & ",365")
.Selected(1) = True
End With


Set download = .Buttons.Add(.Range("f14").Left + 5, Range("f14").Top, 90, 30)
With download
.Name = "download"
.Caption = "下載報表"
.OnAction = "download"
End With

Set download_1 = .Buttons.Add(.Range("h14").Left + 5, Range("h14").Top, 90, 30)
With download_1
.Name = "download_1"
.Caption = "漲跌資料表(日k)"
.OnAction = "download_1"
End With

Set download_2 = .Buttons.Add(.Range("h17").Left + 5, Range("h17").Top, 90, 30)
With download_2
.Name = "download_2"
.Caption = "股利政策"
.OnAction = "download_2"
End With

Set download_3 = .Buttons.Add(.Range("f17").Left + 5, Range("f17").Top, 90, 30)
With download_3
.Name = "download_2"
.Caption = "漲跌資料表(月k)"
.OnAction = "download_3"
End With


.Select
.Cells(5, 2) = "請選擇財務報表": .Cells(5, 4) = "請選擇季表、年表": .Cells(5, 6) = "請選擇年份、季別": .Cells(5, 8) = "股價漲跌資料表"
.Cells(1, 1).Select

End With


End Sub

Sub getpost(url As String, url_a As String, n As String)


Dim HTMLsourcecode As Object, Clipboard As Object, Getxml As Object
Set HTMLsourcecode = CreateObject("htmlfile")
Set Clipboard = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set Getxml = CreateObject("WinHttp.WinHttpRequest.5.1")
With Getxml
.Open "POST", url, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
If url_a <> "" Then .setRequestHeader "Referer", url_a
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.Send

HTMLsourcecode.body.innerhtml = convertraw(.ResponseBody)
If InStr(HTMLsourcecode.body.innertext, "查無股利政策資訊") > 0 Then
MsgBox "查無股利政策資訊"
Exit Sub
End If

If InStr(HTMLsourcecode.body.innertext, "查 無 資 料 !!") > 0 Then
MsgBox "查無資料"
Exit Sub
End If

If InStr(HTMLsourcecode.body.innertext, "查無股價相關資料!!") > 0 Then
MsgBox "查無股價相關資料!!"
Exit Sub
End If



If n <> "股利政策" Then
With Clipboard
If n = "acc" Then .SetText HTMLsourcecode.getelementbyid("divFinDetail").innerhtml
If n = "股價漲跌資料表" Then .SetText HTMLsourcecode.getelementbyid("divPriceDetail").innerhtml
.PutInClipboard
End With
End If




With Sheets(n)
.Select
.Cells.Clear
.Cells(2, 1).Select

If n = "股利政策" Then

Clipboard.SetText HTMLsourcecode.all.tags("table")(10).innerhtml
Clipboard.PutInClipboard
.PasteSpecial NoHTMLFormatting:=True

Clipboard.SetText HTMLsourcecode.getelementbyid("divDetail").innerhtml
Clipboard.PutInClipboard
.Cells(15, 1).Select
.PasteSpecial NoHTMLFormatting:=True

Else

.PasteSpecial NoHTMLFormatting:=True

End If

'標題
.Cells(2, 1).Select
If n = "acc" Then
.Cells(1, 1) = "股票代號:" & Sheets("Goodinfo").Cells(1, 2) & _
"(" & Trim(Split(Sheets("Goodinfo").ListBoxes("list_0").List(Sheets("Goodinfo").ListBoxes("list_0")), ",")(0)) & ")" & _
"(" & Trim(Split(Sheets("Goodinfo").ListBoxes("list_1").List(Sheets("Goodinfo").ListBoxes("list_1")), ",")(0)) & ")" & _
"(" & Trim(Sheets("Goodinfo").ListBoxes("list_2").List(Sheets("Goodinfo").ListBoxes("list_2"))) & ")"
Else
If n = "股價漲跌資料表" Then
If Right(url, 5) = "MONTH" Then
.Cells(1, 1) = "股票代號:" & Sheets("Goodinfo").Cells(1, 2) & "(月K)"
Else
.Cells(1, 1) = "股票代號:" & Sheets("Goodinfo").Cells(1, 2) & _
"(資料範圍:" & Trim(Split(Sheets("Goodinfo").ListBoxes("list_3").List(Sheets("Goodinfo").ListBoxes("list_3")), ",")(0)) & ")"
End If
Else
.Cells(1, 1) = "股票代號:" & Sheets("Goodinfo").Cells(1, 2)
End If
End If
.Cells(1, 1).Font.Bold = True
.Cells(1, 1).Font.Color = -16776961
.Cells(1, 1).Font.Size = 18

End With

End With

'財務比率表(整理)
If Trim(Split(Sheets("Goodinfo").ListBoxes("list_0").List(Sheets("Goodinfo").ListBoxes("list_0")), ",")(0)) = "財務比率表" Then

For i = Sheets("acc").Range("A1").CurrentRegion.Rows.Count - 2 To 2 Step -1
If Sheets("acc").Cells(i, 2) = "" Then
Rows(i & ":" & i).Delete Shift:=xlUp
End If
Next i

End If

Set HTMLsourcecode = Nothing
Set Clipboard = Nothing
Set Getxml = Nothing


End Sub


Sub download()

Dim url As String, url_a As String, stock_id As String

With Sheets("Goodinfo")
.Select
If .Cells(1, 2) = "" Then
stock_id = InputBox("請輸入四碼股票代號" & vbNewLine & "(輸入後,也可以直接修改B1儲存格)")
Else
stock_id = .Cells(1, 2)
End If
If stock_id = "" Then Exit Sub
.Cells(1, 1) = "股票代號"
.Cells(1, 2) = stock_id
.Cells(1, 2).Select

list_0_data = Split(.ListBoxes("list_0").List(.ListBoxes("list_0")), ",")(1)
list_1_data = Split(.ListBoxes("list_1").List(.ListBoxes("list_1")), ",")(1)
list_2_data = Replace(.ListBoxes("list_2").List(.ListBoxes("list_2")), "Q", "")
list_3_data = Split(.ListBoxes("list_3").List(.ListBoxes("list_3")), ",")(1)

url = "https://goodinfo.tw/StockInfo/StockFinDetail.asp?STEP=DATA&STOCK_ID=" & stock_id & "&RPT_CAT=" & list_1_data & "&QRY_TIME=" & list_2_data
url_a = "https://goodinfo.tw/StockInfo/StockFinDetail.asp?RPT_CAT=" & list_0_data & "&STOCK_ID=" & stock_id

End With

Call getpost(url, url_a, "acc")


End Sub



Sub download_1()

Dim url As String, url_a As String, stock_id As String

With Sheets("Goodinfo")
.Select
If .Cells(1, 2) = "" Then
stock_id = InputBox("請輸入四碼股票代號" & vbNewLine & "(輸入後,也可以直接修改B1儲存格)")
Else
stock_id = .Cells(1, 2)
End If
If stock_id = "" Then Exit Sub
.Cells(1, 1) = "股票代號"
.Cells(1, 2) = stock_id
.Cells(1, 2).Select

list_3_data = Split(.ListBoxes("list_3").List(.ListBoxes("list_3")), ",")(1)

url = "https://goodinfo.tw/StockInfo/ShowK_Chart.asp?STOCK_ID=" & stock_id & "&CHT_CAT2=DATE&STEP=DATA.=" & list_3_data
url_a = "https://goodinfo.tw/StockInfo/ShowK_Chart.asp?STOCK_ID=" & stock_id & "&CHT_CAT2=DATE"

End With

Call getpost(url, url_a, "股價漲跌資料表")

End Sub

Sub download_2()

Dim url As String, url_a As String, stock_id As String

With Sheets("Goodinfo")
.Select
If .Cells(1, 2) = "" Then
stock_id = InputBox("請輸入四碼股票代號" & vbNewLine & "(輸入後,也可以直接修改B1儲存格)")
Else
stock_id = .Cells(1, 2)
End If
If stock_id = "" Then Exit Sub
.Cells(1, 1) = "股票代號"
.Cells(1, 2) = stock_id
.Cells(1, 2).Select

url = "https://goodinfo.tw/StockInfo/StockDividendPolicy.asp?STOCK_ID=" & stock_id
url_a = ""

End With

Call getpost(url, url_a, "股利政策")

End Sub


Sub download_3()

Dim url As String, url_a As String, stock_id As String

With Sheets("Goodinfo")
.Select
If .Cells(1, 2) = "" Then
stock_id = InputBox("請輸入四碼股票代號" & vbNewLine & "(輸入後,也可以直接修改B1儲存格)")
Else
stock_id = .Cells(1, 2)
End If
If stock_id = "" Then Exit Sub
.Cells(1, 1) = "股票代號"
.Cells(1, 2) = stock_id
.Cells(1, 2).Select

url = "https://goodinfo.tw/StockInfo/ShowK_Chart.asp?STOCK_ID=" & stock_id & "&CHT_CAT2=MONTH"
url_a = ""

End With

Call getpost(url, url_a, "股價漲跌資料表")

End Sub

Sub list_0_change()

With Sheets("Goodinfo")
.ListBoxes("list_1").List = listdata(.ListBoxes("list_0").ListIndex)
.ListBoxes("list_1").Selected(1) = True
End With
Call list_1_change

End Sub

Sub list_1_change()


With Sheets("Goodinfo")
If InStr(.ListBoxes("list_1").List(.ListBoxes("list_1").ListIndex), "季") = 0 Then
.ListBoxes("list_2").List = listdata(5)
Else
.ListBoxes("list_2").List = listdata(6)
End If
.ListBoxes("list_2").Selected(1) = True
End With

End Sub

Sub list_2_change()

With Sheets("Goodinfo")
'debug
End With

End Sub





Function listdata(choose)

Select Case choose
Case 0
listdata = Array("資產負債表" & Space(30) & ",BS_M_QUAR", "損益表" & Space(30) & ",IS_M_QUAR_ACC", "現金流量表" & Space(30) & ",CF_M_QUAR_ACC", "財務比率表" & Space(30) & ",XX_M_QUAR_ACC")
Case 1
listdata = Array("合併報表 – 單季" & Space(30) & ",BS_M_QUAR", "合併報表 – 年度" & Space(30) & ",BS_M_YEAR", "個別報表 – 單季" & Space(30) & ",BS_QUAR", "個別報表 – 年度" & Space(30) & ",BS_YEAR")
Case 2
listdata = Array("合併報表 – 單季" & Space(30) & ",IS_M_QUAR", "合併報表 – 累季" & Space(30) & ",IS_M_QUAR_ACC", "合併報表 – 年度" & Space(30) & ",IS_M_YEAR", "合併報表 – 近四季" & Space(30) & ",IS_M_Y4Q", "個別報表 – 單季" & Space(30) & ",IS_QUAR", "個別報表 – 累季" & Space(30) & ",IS_QUAR_ACC", "個別報表 – 年度" & Space(30) & ",IS_YEAR", "個別報表 – 近四季" & Space(30) & ",IS_Y4Q")
Case 3
listdata = Array("合併報表 – 單季" & Space(30) & ",CF_M_QUAR", "合併報表 – 累季" & Space(30) & ",CF_M_QUAR_ACC", "合併報表 – 年度" & Space(30) & ",CF_M_YEAR", "合併報表 – 近四季" & Space(30) & ",CF_M_Y4Q", "個別報表 – 單季" & Space(30) & ",CF_QUAR", "個別報表 – 累季" & Space(30) & ",CF_QUAR_ACC", "個別報表 – 年度" & Space(30) & ",CF_YEAR", "個別報表 – 近四季" & Space(30) & ",CF_Y4Q")
Case 4
listdata = Array("合併報表 – 單季" & Space(30) & ",XX_M_QUAR", "合併報表 – 累季" & Space(30) & ",XX_M_QUAR_ACC", "合併報表 – 年度" & Space(30) & ",XX_M_YEAR", "合併報表 – 近四季" & Space(30) & ",XX_M_Y4Q", "個別報表 – 單季" & Space(30) & ",XX_QUAR", "個別報表 – 累季" & Space(30) & ",XX_QUAR_ACC", "個別報表 – 年度" & Space(30) & ",XX_YEAR", "個別報表 – 近四季" & Space(30) & ",XX_Y4Q")
Case 5
listdata = Application.Transpose(Y)
Case 6
listdata = Application.Transpose(Q)
End Select


End Function


Function convertraw(rawdata)

Dim rawstr
Set rawstr = CreateObject("adodb.stream")

With rawstr
.Type = 1
.Mode = 3
.Open
.Write rawdata
.Position = 0
.Type = 2
.Charset = "utf-8"
convertraw = .ReadText
.Close
End With

Set rawstr = Nothing

End Function



'=========================================





20190329 更新(加入標題、股利政策、排版一下財務比率表)
(20190329 09:15)修正一個季別選擇的小bug
(20190329 10:10)搞錯Acer_kewei給我的網址,誤下載到“日K”,新增一個下載“月k”的按鈕
附加壓縮檔: 201903/mobile01-1af67d563ef48943b36438e23958440f.zip



投資理財版的專家Acer_kewei,分享的"台灣五十成份股股價_獲利_淨值_均線變化表"
我加了一些程式碼進去,有興趣請參考
(記得收盤後要多按一下更新,不然資料會留在開盤前、盤中、昨天)
附加壓縮檔: 201903/mobile01-51e75107df5e140033c4a70252479b93.zip
感謝提供. 剛使用一下,發現在啟用巨集後, Goodinfo sheet 的A1 "股票代號"會不見. 在B1輸入公司名稱 (ex 2330),再按我下載,就會恢復. 我用 Excel 2007, Win7 32 bit. 資料都可以看到.. 再次感謝提供好用的程式.

Anyadear wrote:
發現在啟用巨集後, Goodinfo sheet 的A1 "股票代號"會不見...(恕刪)


可以先選好再按下載,程式會自動跳出輸入視窗
有輸入才會出現,是故意的讓它不見的

如果您想要一開始就看見,可以在Workbook_Open(),多加一行
Private Sub Workbook_Open()
Sheets("Goodinfo").cells(1,1)="股票代號"
End Sub
snare大你好

請問最近用大大提供的VBA下載yahoo finance歷史資料會變得不穩和變慢
大大有遇過嗎

謝謝
好文一篇,感謝樓主熱心分享

peter624 wrote:
下載yahoo finance歷史資料會變得不穩和變慢...(恕刪)


網路問題吧…

剛試了一下,速度還可以




大大是我程式的問題 謝謝
把Acer_kewei 的表格+網址,合併成一個檔案,早上臨時寫,有bug請無視

表單排版超隨便,也請無視
如果有人要重排一個表單給我這個美術白癡,非常歡迎,我再修改

按看看,1次 or 2次





'===================================
'範例說明:
'610樓的表格+613樓的程式=>合併
'開出來的表單,可拉到旁邊放著,不影響excel操作
'程式內的股票代號,跟工作表中的同步,如果有改變會自動修改表單
'===================================
'thisworkbook
Private Sub Workbook_Open()
Sheets("acc").Cells.Clear
Sheets("股價漲跌資料表").Cells.Clear
Sheets("股利政策").Cells.Clear
Call get_Q_Y
' If ((Not Not Q) = 0) Or ((Not Not Y) = 0) Then
' Debug.Print "network error"
' End If
'UserForm1.Show 0

End Sub

'userform1===================================


Private Sub CommandButton1_Click()
Dim url As String, url_a As String
If ListBox3.List(0) = "error" Then
MsgBox "網路異常,請按手動更新,重試一次"
CommandButton5.Visible = True
Exit Sub
End If

If TextBox1.Text <> "" Then

url = "https://goodinfo.tw/StockInfo/StockFinDetail.asp?STEP=DATA&STOCK_ID=" & TextBox1.Text & _
"&RPT_CAT=" & Split(UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex), ",")(1) & _
"&QRY_TIME=" & Replace(UserForm1.ListBox3.List(UserForm1.ListBox3.ListIndex, 0), "Q", "")

url_a = "https://goodinfo.tw/StockInfo/StockFinDetail.asp?RPT_CAT=" & _
Split(UserForm1.ListBox1.List(UserForm1.ListBox1.ListIndex), ",")(1) & _
"&STOCK_ID=" & TextBox1.Text


Call getpost(url, url_a, "acc")

Else
MsgBox "請在清單選擇或在textbox輸入股票代號"
End If

End Sub

Private Sub CommandButton2_Click()

Dim url As String, url_a As String

If ListBox3.List(0) = "error" Then
MsgBox "網路異常,請按手動更新,重試一次"
CommandButton5.Visible = True
Exit Sub
End If

If TextBox1.Text <> "" Then

url = "https://goodinfo.tw/StockInfo/ShowK_Chart.asp?STOCK_ID=" & TextBox1.Text & _
"&CHT_CAT2=DATE&STEP=DATA&PERIOD=" & Split(UserForm1.ListBox4.List(UserForm1.ListBox4.ListIndex), ",")(1)
url_a = "https://goodinfo.tw/StockInfo/ShowK_Chart.asp?STOCK_ID=" & TextBox1.Text & "&CHT_CAT2=DATE"

Call getpost(url, url_a, "股價漲跌資料表")

Else
MsgBox "請在清單選擇或在textbox輸入股票代號"
End If


End Sub

Private Sub CommandButton3_Click()

Dim url As String, url_a As String

If ListBox3.List(0) = "error" Then
MsgBox "網路異常,請按手動更新,重試一次"
CommandButton5.Visible = True
Exit Sub
End If

If TextBox1.Text <> "" Then

url = "https://goodinfo.tw/StockInfo/ShowK_Chart.asp?STOCK_ID=" & TextBox1.Text & "&CHT_CAT2=MONTH"
url_a = ""
Call getpost(url, url_a, "股價漲跌資料表")

Else
MsgBox "請在清單選擇或在textbox輸入股票代號"
End If

End Sub

Private Sub CommandButton4_Click()

Dim url As String, url_a As String

If ListBox3.List(0) = "error" Then
MsgBox "網路異常,請按手動更新,重試一次"
CommandButton5.Visible = True
Exit Sub
End If

If TextBox1.Text <> "" Then

url = "https://goodinfo.tw/StockInfo/StockDividendPolicy.asp?STOCK_ID=" & TextBox1.Text
url_a = ""
Call getpost(url, url_a, "股利政策")

Else
MsgBox "請在清單選擇或在textbox輸入股票代號"
End If


End Sub

Private Sub CommandButton5_Click()

Call get_Q_Y
If ((Not Not Y) = 0) Or ((Not Not Q) = 0) Then
Debug.Print "network error"
Else
CommandButton5.Visible = False
ListBox3.List = listdata(6)
End If

ListBox1.ListIndex = 0
ListBox2.ListIndex = 0
ListBox3.ListIndex = 0
ListBox4.ListIndex = 0


End Sub

Private Sub ListBox1_Click()

With ListBox1
ListBox2.List = listdata(ListBox1.ListIndex + 1)
ListBox2.ListIndex = 0
End With
'Call ListBox2_Click

End Sub

Private Sub ListBox2_Click()

With ListBox3
If InStr(ListBox2.List(ListBox2.ListIndex), "季") = 0 Then
.List = listdata(5)
Else
.List = listdata(6)
End If
.ListIndex = 0
End With


End Sub


Private Sub ListBox5_Click()
Select Case ListBox5.List(ListBox5.ListIndex)
Case Is = "台灣50"
ListBox6.List = Sheets("台灣50").Range("a4:b54").Value
Case Is = "中型100"
ListBox6.List = Sheets("中型100").Range("a4:b101").Value
Case Is = "自訂"
If Sheets("自訂").Range("A1").CurrentRegion.Rows.Count >= 2 Then
ListBox6.List = Sheets("自訂").Range("a2:b" & Sheets("自訂").Range("A1").CurrentRegion.Rows.Count).Value
End If
End Select
ListBox6.ListIndex = 0

End Sub

Private Sub ListBox6_Click()

TextBox1.Text = UserForm1.ListBox6.List(UserForm1.ListBox6.ListIndex, 0)

End Sub

Private Sub UserForm_Initialize()
Call addlistbox_new
ListBox1.ListIndex = 0
ListBox2.ListIndex = 0
ListBox3.ListIndex = 0
ListBox4.ListIndex = 0
ListBox5.ListIndex = 2
ListBox6.ListIndex = 0
End Sub


'module1================================
Public Q(), Y()

Sub open_form()
If UserForm1.Visible = True Then
UserForm1.Hide
Else
UserForm1.Show 0
End If
' Call get_Q_Y
' UserForm1.Show 0
End Sub

Sub get_Q_Y() '季別、年度


Dim HTMLsourcecode As Object, Getxml As Object, Get_Q, Get_Y, i As Integer, j As Integer
Set HTMLsourcecode = CreateObject("htmlfile")
Set Getxml = CreateObject("WinHttp.WinHttpRequest.5.1")

url = "https://goodinfo.tw/StockInfo/StockFinDetail.asp?RPT_CAT=BS_M_QUAR&STOCK_ID=2412"
url_a = "https://goodinfo.tw/StockInfo/StockFinDetail.asp?STEP=DATA&STOCK_ID=2412&RPT_CAT=BS_M_YEAR&QRY_TIME=20184"
On Error Resume Next

For i = 1 To 2

With Getxml
.Open "POST", choose(i, url, url_a), False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
If i = 2 Then .setRequestHeader "Referer", url
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.Send

HTMLsourcecode.body.innerhtml = convertraw(.ResponseBody)
If InStr(HTMLsourcecode.body.innertext, "查 無 資 料 !!") > 0 Then
'MsgBox "查無資料"
Exit Sub
End If

If i = 1 Then
Set Get_Q = HTMLsourcecode.getelementbyid("QRY_TIME")
Debug.Print Get_Q.Length

ReDim Q(Get_Q.Length - 1)

For j = 0 To Get_Q.Length - 1
Q(j) = Get_Q(j).innertext
Next j

Else
Set Get_Y = HTMLsourcecode.getelementbyid("QRY_TIME")
Debug.Print Get_Y.Length

ReDim Y(Get_Y.Length - 1)


For j = 0 To Get_Y.Length - 1
Y(j) = Left(Get_Y(j).innertext, 4)
Next j
End If

End With

Next i



Set HTMLsourcecode = Nothing
Set Getxml = Nothing
Set Get_Q = Nothing
Set Get_Y = Nothing



End Sub



Sub getpost(url As String, url_a As String, n As String)


Dim HTMLsourcecode As Object, Clipboard As Object, Getxml As Object
Set HTMLsourcecode = CreateObject("htmlfile")
Set Clipboard = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set Getxml = CreateObject("WinHttp.WinHttpRequest.5.1")
On Error Resume Next

With Getxml
.Open "POST", url, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
If url_a <> "" Then .setRequestHeader "Referer", url_a
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.Send

HTMLsourcecode.body.innerhtml = convertraw(.ResponseBody)
If InStr(HTMLsourcecode.body.innertext, "查無股利政策資訊") > 0 Then
MsgBox "查無股利政策資訊"
Exit Sub
End If

If InStr(HTMLsourcecode.body.innertext, "查 無 資 料 !!") > 0 Then
MsgBox "查無資料"
Exit Sub
End If

If InStr(HTMLsourcecode.body.innertext, "查無股價相關資料!!") > 0 Then
MsgBox "查無股價相關資料!!"
Exit Sub
End If



If n <> "股利政策" Then
With Clipboard
If n = "acc" Then .SetText HTMLsourcecode.getelementbyid("divFinDetail").innerhtml
If n = "股價漲跌資料表" Then .SetText HTMLsourcecode.getelementbyid("divPriceDetail").innerhtml
.PutInClipboard
End With
End If




With Sheets(n)
.Select
.Cells.Clear
.Cells(2, 1).Select

If n = "股利政策" Then

Clipboard.SetText HTMLsourcecode.all.tags("table")(10).innerhtml
Clipboard.PutInClipboard
.PasteSpecial NoHTMLFormatting:=True

Clipboard.SetText HTMLsourcecode.getelementbyid("divDetail").innerhtml
Clipboard.PutInClipboard
.Cells(15, 1).Select
.PasteSpecial NoHTMLFormatting:=True

Else

.PasteSpecial NoHTMLFormatting:=True

End If

'標題
If n = "acc" Then
.Cells(1, 1) = "股票代號:" & UserForm1.TextBox1.Text & _
"(" & Trim(Split(UserForm1.ListBox1.List(UserForm1.ListBox1.ListIndex), ",")(0)) & ")" & _
"(" & Trim(Split(UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex), ",")(0)) & ")" & _
"(" & UserForm1.ListBox3.List(UserForm1.ListBox3.ListIndex) & ")"
Else
If n = "股價漲跌資料表" Then
If Right(url, 5) = "MONTH" Then
.Cells(1, 1) = "股票代號:" & UserForm1.TextBox1.Text & "(月K)"
Else
.Cells(1, 1) = "股票代號:" & UserForm1.TextBox1.Text & _
"(資料範圍:" & Trim(Split(UserForm1.ListBox4.List(UserForm1.ListBox4.ListIndex), ",")(0)) & ")"
End If
Else
.Cells(1, 1) = "股票代號:" & UserForm1.TextBox1.Text
End If
End If
.Cells(1, 1).Font.Bold = True
.Cells(1, 1).Font.Color = -16776961
.Cells(1, 1).Font.Size = 18
.Cells(2, 1).Select
End With

End With

'財務比率表(整理)
If Trim(Split(UserForm1.ListBox1.List(UserForm1.ListBox1.ListIndex), ",")(0)) = "財務比率表" Then

For i = Sheets("acc").Range("A1").CurrentRegion.Rows.Count - 2 To 2 Step -1
If Sheets("acc").Cells(i, 2) = "" Then
Rows(i & ":" & i).Delete Shift:=xlUp
End If
Next i

End If

Set HTMLsourcecode = Nothing
Set Clipboard = Nothing
Set Getxml = Nothing


End Sub

Function listdata(choose)

Select Case choose
Case 0
listdata = Array("資產負債表" & Space(30) & ",BS_M_QUAR", "損益表" & Space(30) & ",IS_M_QUAR_ACC", "現金流量表" & Space(30) & ",CF_M_QUAR_ACC", "財務比率表" & Space(30) & ",XX_M_QUAR_ACC")
Case 1
listdata = Array("合併報表 – 單季" & Space(30) & ",BS_M_QUAR", "合併報表 – 年度" & Space(30) & ",BS_M_YEAR", "個別報表 – 單季" & Space(30) & ",BS_QUAR", "個別報表 – 年度" & Space(30) & ",BS_YEAR")
Case 2
listdata = Array("合併報表 – 單季" & Space(30) & ",IS_M_QUAR", "合併報表 – 累季" & Space(30) & ",IS_M_QUAR_ACC", "合併報表 – 年度" & Space(30) & ",IS_M_YEAR", "合併報表 – 近四季" & Space(30) & ",IS_M_Y4Q", "個別報表 – 單季" & Space(30) & ",IS_QUAR", "個別報表 – 累季" & Space(30) & ",IS_QUAR_ACC", "個別報表 – 年度" & Space(30) & ",IS_YEAR", "個別報表 – 近四季" & Space(30) & ",IS_Y4Q")
Case 3
listdata = Array("合併報表 – 單季" & Space(30) & ",CF_M_QUAR", "合併報表 – 累季" & Space(30) & ",CF_M_QUAR_ACC", "合併報表 – 年度" & Space(30) & ",CF_M_YEAR", "合併報表 – 近四季" & Space(30) & ",CF_M_Y4Q", "個別報表 – 單季" & Space(30) & ",CF_QUAR", "個別報表 – 累季" & Space(30) & ",CF_QUAR_ACC", "個別報表 – 年度" & Space(30) & ",CF_YEAR", "個別報表 – 近四季" & Space(30) & ",CF_Y4Q")
Case 4
listdata = Array("合併報表 – 單季" & Space(30) & ",XX_M_QUAR", "合併報表 – 累季" & Space(30) & ",XX_M_QUAR_ACC", "合併報表 – 年度" & Space(30) & ",XX_M_YEAR", "合併報表 – 近四季" & Space(30) & ",XX_M_Y4Q", "個別報表 – 單季" & Space(30) & ",XX_QUAR", "個別報表 – 累季" & Space(30) & ",XX_QUAR_ACC", "個別報表 – 年度" & Space(30) & ",XX_YEAR", "個別報表 – 近四季" & Space(30) & ",XX_Y4Q")
Case 5
If ((Not Not Y) = 0) Then
listdata = Array("error")
Else
listdata = Application.Transpose(Y)
End If
Case 6
If ((Not Not Q) = 0) Then
listdata = Array("error")
Else
listdata = Application.Transpose(Q)
End If
End Select


End Function


Sub addlistbox_new()

With UserForm1.ListBox1
.List = listdata(0)
End With

With UserForm1.ListBox2
.List = listdata(1)
End With

With UserForm1.ListBox3
.List = listdata(6)
End With

With UserForm1.ListBox4
.List = Array("三個月" & Space(30) & ",90", "六個月" & Space(30) & ",180", "一年" & Space(30) & ",365")
End With

With UserForm1.ListBox5
.List = Array("台灣50", "中型100", "自訂")
End With

With UserForm1.ListBox6
.List = Sheets("台灣50").Range("a4:b54").Value
End With


End Sub

Function convertraw(rawdata)

Dim rawstr
Set rawstr = CreateObject("adodb.stream")

With rawstr
.Type = 1
.Mode = 3
.Open
.Write rawdata
.Position = 0
.Type = 2
.Charset = "utf-8"
convertraw = .ReadText
.Close
End With

Set rawstr = Nothing

End Function



Sub get_0050持股明細()

Dim url As String, HTMLsourcecode As Object, Getxml As Object, i As Integer, j As Integer, Update_Day As String
Set HTMLsourcecode = CreateObject("htmlfile")
Set Getxml = CreateObject("msxml2.xmlhttp")
url = "https://www.moneydj.com/ETF/X/Basic/Basic0007A.xdjhtm?etfid=0050.TW"
Application.ScreenUpdating = False

With Getxml

.Open "GET", url, False
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.Send

HTMLsourcecode.body.innerhtml = .responsetext


With Sheets("台灣五十成份股比例")
.Cells.Clear

Update_Day = HTMLsourcecode.getelementbyid("ctl00_ctl00_MainContent_MainContent_sdate2").innertext
.Cells(1, 1) = "持股分佈 (依產業)" & "(" & Update_Day & ")"

Set Table = HTMLsourcecode.all.tags("table")(4).Rows
For i = 0 To Table.Length - 1
For j = 1 To Table(i).Cells.Length - 1
.Cells(i + 2, j) = Table(i).Cells(j).innertext
Next j
Next i

Update_Day = HTMLsourcecode.getelementbyid("ctl00_ctl00_MainContent_MainContent_sdate3").innertext
.Cells(1, 5) = "元大台灣卓越50基金-持股明細" & "(" & Update_Day & ")"
Set Table = HTMLsourcecode.all.tags("table")(5).Rows
For i = 0 To Table.Length - 1
For j = 0 To Table(i).Cells.Length - 1
.Cells(i + 2, j + 5) = Table(i).Cells(j).innertext
Next j
Next i

Set Table = HTMLsourcecode.all.tags("table")(6).Rows
For i = 0 To Table.Length - 1
For j = 0 To Table(i).Cells.Length - 1
.Cells(i + 2, j + 9) = Table(i).Cells(j).innertext
Next j
Next i


End With


End With

Call SetFormatCondition("台灣五十成份股比例", "H3")
Call SetFormatCondition("台灣五十成份股比例", "L3")

Set HTMLsourcecode = Nothing
Set Getxml = Nothing

Application.ScreenUpdating = True


End Sub

Sub get_0050()

Dim n As String, url As String, HTMLsourcecode As Object, Getxml As Object, i As Integer, check_color As String, check_Column As Integer
Set HTMLsourcecode = CreateObject("htmlfile")
Set Getxml = CreateObject("msxml2.xmlhttp")
url = "https://tw.stock.yahoo.com/q/q?s="
n = "台灣50"

Application.ScreenUpdating = False
On Error Resume Next

i = 1
Do Until Sheets(n).Cells(2, i) = "成交"
i = i + 1
Loop
check_Column = i

If Format(Date, "yyyymmdd") <> Trim(Sheets(n).Cells(1, check_Column)) Then
Sheets(n).Columns(check_Column).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Sheets(n).Cells(1, check_Column) = Format(Date, "yyyymmdd")
Sheets(n).Columns(check_Column).AutoFit
Sheets(n).Cells(2, check_Column) = "成交"
End If

With Getxml
.Open "GET", "https://tw.stock.yahoo.com/", False
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.Send
Sheets(n).Cells(3, check_Column) = Split(Split(.responsetext, "class=""dx"">")(1), "")(0)
End With



For i = 4 To 54


With Getxml

.Open "GET", url & Sheets(n).Cells(i, 1), False
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.Send

HTMLsourcecode.body.innerhtml = .responsetext

With Sheets(n)
.Cells(i, check_Column) = HTMLsourcecode.all.tags("table")(2).Rows(1).Cells(2).innertext
check_color = HTMLsourcecode.all.tags("table")(2).Rows(1).Cells(5).innertext
If InStr(check_color, "▽") > 0 Or InStr(check_color, "▼") > 0 Then .Cells(i, check_Column).Font.Color = -11489280
If InStr(check_color, "△") > 0 Or InStr(check_color, "▲") > 0 Then .Cells(i, check_Column).Font.Color = -16776961
End With

End With

Next i


Set HTMLsourcecode = Nothing
Set Getxml = Nothing

Application.ScreenUpdating = True

End Sub

Sub get_100()

Dim n As String, url As String, HTMLsourcecode As Object, Getxml As Object, i As Integer, check_color As String
Set HTMLsourcecode = CreateObject("htmlfile")
Set Getxml = CreateObject("msxml2.xmlhttp")
url = "https://tw.stock.yahoo.com/q/q?s="
n = "中型100"

Application.ScreenUpdating = False
On Error Resume Next

If Format(Date, "yyyymmdd") <> Trim(Sheets(n).Cells(1, 4)) Then
Sheets(n).Columns(4).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Sheets(n).Cells(1, 4) = Format(Date, "yyyymmdd")
Sheets(n).Columns(4).AutoFit
Sheets(n).Cells(2, 4) = "成交"
End If


For i = 4 To 101


With Getxml

.Open "GET", url & Sheets(n).Cells(i, 1), False
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.Send

HTMLsourcecode.body.innerhtml = .responsetext

With Sheets(n)
.Cells(i, 4) = HTMLsourcecode.all.tags("table")(2).Rows(1).Cells(2).innertext
check_color = HTMLsourcecode.all.tags("table")(2).Rows(1).Cells(5).innertext
If InStr(check_color, "▽") > 0 Or InStr(check_color, "▼") > 0 Then .Cells(i, 4).Font.Color = -11489280
If InStr(check_color, "△") > 0 Or InStr(check_color, "▲") > 0 Then .Cells(i, 4).Font.Color = -16776961
End With

End With

Next i


Set HTMLsourcecode = Nothing
Set Getxml = Nothing

Application.ScreenUpdating = True

End Sub

Sub SetFormatCondition(n As String, r As String)

Dim Crange As Range, C1 As FormatCondition, C2 As FormatCondition
Set Crange = Sheets(n).Range(r, Sheets(n).Range(r).End(xlDown))
Crange.FormatConditions.Delete
Crange.Font.Bold = True
Set C1 = Crange.FormatConditions.Add(xlCellValue, xlGreater, "=0")
C1.Font.Color = vbRed
Set C2 = Crange.FormatConditions.Add(xlCellValue, xlLess, "=0")
C2.Font.Color = -11489280
Set Crange = Nothing

End Sub


'===================================


'(20190407 20:20更新,感謝yuhuahsiao(632樓)提醒漏了一個表格沒下載
'請自行加入程式碼或重新下載檔案

附加壓縮檔: 201904/mobile01-152b62ba1b45ecbaa2af9cd8a578a92c.zip
關閉廣告
文章分享
評分
評分
複製連結
請輸入您要前往的頁數(1 ~ 144)

今日熱門文章 網友點擊推薦!