- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
56#
發表於 2014-5-17 17:29
| 只看該作者
回復 55# smart3135
加了a=Q.Cells(1)有什麼不同呢?雖然執行時不會出錯,不過結果還是沒變,
加了a=Q.Cells(1),只是為了顯示於圖示:區域變數視窗中的不可見字元- Sub 上市月成交資訊()
- Dim Rng As Range, Rng1 As Range, E As Range, X As Range, T As Date, xPath As String, xFile As String
- Dim Ea As Variant, ar(), ii As Integer
- T = Time
- Application.DisplayStatusBar = True
- '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
- Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
- Set Rng1 = ThisWorkbook.Sheets(3).Range("b:b")
- If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
- If Application.Count(Rng1) = 0 Then MsgBox "沒有股票代號": Exit Sub
- Set Rng = Rng.SpecialCells(xlCellTypeConstants)
- Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants)
- xPath = "D:\財報資料"
- IE_Application '
- Application.StatusBar = " "
- For Each E In Rng
- With Sheets(1)
- .Activate
- .Cells.Clear '下載資料置於此工作表,變換股票時:清空
- End With
- For Each X In Rng1
- With IE
- .Document.getElementsByTagName("select")("myear").Value = X
- With .Document.getelementbyid("STK_NO")
- .Value = E
- .Document.getelementSbyNAME("login_btn")(0).Click '按下查詢
- End With
- Do While .Busy Or .readyState <> 4: Loop
- If .Document.getElementsByTagName("TABLE")(7).Rows.Length > 1 Then
- Ep .Document.getElementsByTagName("TABLE")(7).outerHTML
- Else
- GoTo Nn
- End If
- End With
- Next X
- Nn:
- xFile = xPath & "\" & E & "\HPM.txt"
- MkDir_Sub xFile
- Maketxt xFile, Sheets(1).Range("A1").CurrentRegion, E.Value
- ii = ii + 1
- Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii & " 文字檔"
- Next E
- IE.Quit
- Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii & " 文字檔, 讀取完畢 !! "
- MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
- ' ThisWorkbook.Save
- End Sub
複製代碼- Option Explicit
- Dim IE As Object
- Sub IE_Application()
- If Not IE Is Nothing Then IE.Quit '當查到上櫃的年份就會出現查無,此IE無法再度查詢,關閉它
- Set IE = CreateObject("InternetExplorer.Application")
- With IE
- .Navigate "http://www.gretai.org.tw/ch/stock/statistics/monthly/st44.php"
- .Visible = True '不顯示ie
- Do While .Busy Or .readyState <> 4: DoEvents: Loop
- End With
- End Sub
- Sub 上櫃月成交資訊()
- Dim Rng As Range, Rng1 As Range, E As Range, X As Range, T As Date, xPath As String, xFile As String
- Dim Ea As Variant, AR(), ii As Integer, Msg As Boolean
- T = Time
- Application.DisplayStatusBar = True
- '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
- Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
- Set Rng1 = ThisWorkbook.Sheets(3).Range("b:b")
- If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
- If Application.Count(Rng1) = 0 Then MsgBox "沒有股票代號": Exit Sub
- Set Rng = Rng.SpecialCells(xlCellTypeConstants)
- Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants)
- xPath = "D:\財報資料"
- Application.StatusBar = " "
- For Each E In Rng
- If Msg = False Then IE_Application '當查到上櫃的年份就會出現查無,重開IE
- Sheets(1).UsedRange.Clear '下載資料置於此工作表,變換股票時:清空
- For Each X In Rng1
- With IE
- .document.getElementsByTagName("select")("yy").Value = X
- Do While .Busy Or .readyState <> 4: Loop
- With .document.getelementbyid("input_stock_code")
- .Value = E
- .ParentNode.submit
- End With
- Do While .Busy Or .readyState <> 4: Loop
- If InStr(.document.getElementsByTagName("TABLE")(0).innerHTML, "查無該筆資料") = 0 Then
- Msg = True
- If Application.Count(Sheets(1).UsedRange) = 0 Then '此工作表清空時:下載第一年度時
- AR = Array(0, 2)
- Else
- AR = Array(2)
- End If
- For Each Ea In AR
- Ep .document.getElementsByTagName("TABLE")(Ea).outerHTML
- Next
- Else
- Msg = False '上櫃的年份就會出現查無
- GoTo NN
- End If
- ii = ii + 1
- End With
- Next X
- NN:
- xFile = xPath & "\" & E & "\HPM.txt"
- MkDir_Sub xFile
- Maketxt xFile, Sheets(1).UsedRange
- Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii / 3 & " 文字檔"
- Next E
- IE.Quit
- Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii / 3 & " 文字檔, 讀取完畢 !! "
- MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
- End Sub
- Sub Maketxt(xF As String, Q As Range) '將匯入資料存入指定的txt
- Dim fs As Object, E As Range, C As Variant
- Q.Range("C1") = ""
- Q.Range("A1") = Q.Range("B1") & " " & "月成交資料"
- Q.Range("B1") = ""
- Q.Range("a4", Q.Range("a4").End(xlDown)).Replace "年", ""
- Q.Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
- Q.Rows(3).Delete '上櫃月成交資料當月還未結束時就會有資料了,所以要將還沒結束的月份刪除
- '是Q.Rows(3)不 Rows(4)
- Set fs = CreateObject("Scripting.FileSystemObject")
- Set fs = fs.CreateTextFile(xF, True) '創見一個檔案,如檔案存在可覆蓋掉
- For Each E In Q.Rows
- C = Application.Transpose(Application.Transpose(E.Value))
- C = Join(C, vbTab)
- fs.WriteLine C
- Next
- fs.Close
- End Sub
複製代碼 |
|