- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
4#
發表於 2014-1-15 19:22
| 只看該作者
回復 1# jasonwu0114
自製看盤
ThisWorkbook 程式碼- Option Explicit
- Dim ie() As Object
- Const Sh = "看盤" '指定工作表名稱
- Private Sub Workbook_Open()
- Dim i As Integer, T As Date
- Workbook_BeforeClose False
- With Sheets(Sh)
- Application.StatusBar = "網頁下載中..."
- For i = 2 To .UsedRange.Columns(1).Rows.Count '股票代號
- ReDim Preserve ie(2 To i)
- If .Cells(i, "A") <> "" And IsNumeric(.Cells(i, "A")) And Len(.Cells(i, "A")) >= 4 Then
- Set ie(i) = CreateObject("InternetExplorer.Application")
- ie(i).Navigate "http://newmis.twse.com.tw/stock/fibest.jsp?stock=" & .Cells(i, "A")
- DoEvents
- Application.StatusBar = "下載... " & .Cells(i, "A")
- ie(i).Visible = False
- Else
- Set ie(i) = Nothing
- .UsedRange.Rows(i).Offset(, 1) = ""
- End If
- Next
- End With
- T = Time + #12:00:05 AM#
- If Time < #9:00:00 AM# Then T = #9:00:00 AM#
- Application.OnTime T, "ThisWorkbook.所有股價"
- End Sub
- Private Sub 即時股價(R As Integer)
- Dim Element As Object, C As Integer
- Application.EnableEvents = False
- With Sheets(Sh)
- If Not ie(R) Is Nothing Then
- With ie(R)
- Do While .Busy Or .ReadyState <> 4: Loop
- Set Element = .document.getElementsByTagName("TABLE")(1) '
- End With
- For C = 0 To Element.Rows(1).Cells.Length - 1
- .Cells(R, C + 2) = Element.Rows(1).Cells(C).innertext
- Next
- Else
- .UsedRange.Rows(R).Offset(, 1) = ""
- End If
- End With
- Application.EnableEvents = True
- End Sub
- Private Sub 所有股價()
- Dim R As Integer
- If Time >= #1:30:00 PM# Then
- Workbook_BeforeClose False
- Application.StatusBar = " 已收盤!!!"
- Exit Sub
- End If
- For R = 2 To UBound(ie)
- 即時股價 R
- Next
- Application.OnTime Time + #12:00:02 AM#, "ThisWorkbook.所有股價"
- Application.StatusBar = Time & vbTab & "更新完成"
- End Sub
- Private Sub Workbook_BeforeClose(Cancel As Boolean)
- Dim e As Variant
- On Error Resume Next
- For Each e In ie
- e.Quit
- Set e = Nothing
- Next
- End Sub
- Private Sub Workbook_SheetChange(ByVal Wsh As Object, ByVal Target As Range)
- If Wsh.Name = Sh Then
- If Target.Column = 1 And Target.Row > 1 Then Workbook_Open
- End If
- End Sub
複製代碼 |
|