- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
回復 6# wufonna
試試看- Option Explicit
- Dim ie As Object '模組最頂端 Dim 供這模組的程序使用的變數
- Sub AllFile()
- Dim i As Integer, v, Y As Integer, S As String
- S = "I" '2005 年度起始欄位
- Set ie = CreateObject("internetexplorer.application") '使用此方式可以免除 "設定引用項目"
- With ie '縮小IE視窗
- .Visible = True
- .Width = 5
- .Height = 5
- End With
- With 工作表1
- .Range(S & 1).CurrentRegion = "" '清除工作表1,年度範圍
- Do
- .Range(S & 1).Offset(, Y) = 2005 + Y
- Y = Y + 1
- Loop While 2005 + Y < Year(Date)
- For i = 2 To .Range("A" & .Rows.Count).End(xlUp).Row
- v = .Cells(i, 1).Value
- GetDividend (v)
- .Cells(i, S).Resize(, Y) = Application.WorksheetFunction.Transpose(工作表2.Range("H4").Resize(Y))
- If Application.Sum(.Cells(i, S).Resize(, Y)) > 1 Then
- .Cells(i, S).Offset(, Y) = 1
- Else
- .Cells(i, S).Offset(, Y) = 0
- End If
- Next
- .Range(S & 1).CurrentRegion.Replace "--", "", xlWhole
- End With
- With ie 'IE視窗最大化
- Application.WindowState = xlMaximized
- .Height = Application.Height
- .Width = Application.Width
- .Quit
- End With
- End Sub
- Private Sub GetDividend(ByVal ss As String)
- Dim rr As String, T As Date, i, ii, k, j, S
- On Error Resume Next
- T = Time
- rr = "http://yamstock.megatime.com.tw/asp/stockinfo/GetReport.asp?select_table=html\Ficxxx\&stockid=" & ss & "&name1=D4&index1=12" ' 正確的
- With ie
- .Navigate rr
- Do While .ReadyState <> 4 '等待網頁下載完畢
- DoEvents
- If Time >= T + #12:00:03 AM# Then '等待3秒
- DoEvents
- Application.SendKeys "~" '股票代號錯誤,網頁會有訊息,須按確定,才可繼續下面股票代號
- Exit Do
- End If
- Loop
- Set S = .Document.getelementsbytagname("table")(11)
- With 工作表2
- .UsedRange.Clear
- For i = 0 To S.Rows.Length - 1 '寫入資料
- k = k + 1
- For j = 0 To S.Rows(i).Length - 1
- For ii = 0 To S.Rows(i).Cells.Length - 1
- .Cells(k, ii + 1) = S.Rows(i).Cells(ii).innertext
- DoEvents
- Next
- Next
- Next
- End With
- End With
- End Sub
複製代碼 |
|