- 帖子
- 96
- 主題
- 18
- 精華
- 0
- 積分
- 125
- 點名
- 0
- 作業系統
- Windows 7
- 軟體版本
- Office 2007
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2014-3-23
- 最後登錄
- 2022-8-2
|
63#
發表於 2014-5-21 09:28
| 只看該作者
回復 62# GBKEE
版主您好,不好意思,我有試著將For x = A - 1 To A 改成For x = 0 To A,不過出錯訊息和出錯位置仍相同,能不能再麻煩您測試一下呢?
另外在更之前的上櫃年成交資料,就是用比較舊的寫法,不貼上EXCEL直接寫入TXT的程式碼,雖然有資料,不過最上方少了個股代號和名稱,不知這部分能不能
也擷取到資料並寫入TXT?或是是有可以先貼到EXCEL再寫入TXT的方法?再麻煩您了!- Option Explicit
- Dim IE As Object, A As Integer
- Sub IE_Application()
- Dim I As Integer
- Set IE = CreateObject("InternetExplorer.Application")
- With IE
- .Navigate "http://www.tdcc.com.tw/smWeb/QryStock.jsp"
- .Visible = True '不顯示ie
- Do While .Busy Or .readyState <> 4: DoEvents: Loop
- '讀取集保戶股權分散表查詢的資料日期總個數
- A = .document.getelementsByTagName("select")("SCA_DATE").Length - 1
- End With
- End Sub
- Sub 集保()
- Dim Rng As Range, E As Range, x As Variant, T As Date, xPath As String, xFile As String
- Dim Ea As Variant, ii As Integer
- T = Time
- Application.DisplayStatusBar = True
- '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
- Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
- If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
- Set Rng = Rng.SpecialCells(xlCellTypeConstants)
- xPath = "D:\財報資料"
- IE_Application '
- Application.StatusBar = " "
- For Each E In Rng
- With Sheets(1)
- .Activate
- .Cells.Clear '下載資料置於此工作表,變換股票時:清空
- End With
- For x = 0 To A
- With IE
- .document.getelementsByTagName("select")("SCA_DATE")(x).Selected = True
- '這裡的資料日期 需用 Select 它是有[年度月份日期]的
- .document.getElementById("StockNo").Value = E
- .document.getelementsByTagName("INPUT")("sub").Click '按下查詢
- '這裡的查詢是<INPUT TYPE="submit" VALUE="查詢" name="sub">
- 'End With
- Do While .Busy Or .readyState <> 4: Loop
- Ep .document.getelementsByTagName("TABLE")(7).outerHTML
- End With
- Next x
- xFile = xPath & "\" & E & "\SHD.txt"
- MkDir_Sub xFile
-
- Maketxt xFile, Sheets(1).Range("A1").CurrentRegion, E.Value
- '***想請問您Code As String的Code是怎麼帶出個股編號的 ****
- '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
- Sub Ep(S As String)
- Dim D As New DataObject, E As Shape, FormDLL As String, Rng As Range
- 'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
- '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
- On Error GoTo ER
- With D
- .SetText S
- .PutInClipboard
- With Sheets(1)
- .Range("a" & .Rows.Count).End(xlUp).Offset(1).Select
- .PasteSpecial Format:="Unicode 文字"
- ' Set Rng = Selection
- ' Rng.Sort Key1:=Rng.Range("B2"), Order1:=xlDescending, Header:=xlYes ', _
- OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
- :=xlStroke, DataOption1:=xlSortNorma
- End With
- End With
- Exit Sub
- ER:
- FormDLL = "FM20.DLL"
- ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\" & FormDLL
- Resume
- End Sub
- Sub Maketxt(xF As String, Q As Range, Code As String) '將匯入資料存入指定的txt
- '***想請問您Code As String的Code是怎麼帶出個股編號的 ****
- ' xF(接收的引數名稱) As String(字串型態), Q As Range(Range物件型態), Code As String(字串型態)
- Dim fs As Object, E As Range, C As Variant
- 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
- Sub MkDir_Sub(S As String)
- Dim AR, I As Integer, xPath As String
- If Dir(S) = "" Then
- AR = Split(S, "\")
- xPath = AR(0)
- For I = 1 To UBound(AR) - 1
- xPath = xPath & "\" & AR(I)
- If Dir(xPath, vbDirectory) = "" Then MkDir xPath
- Next
- End If
- End Sub
複製代碼- Option Explicit
- Dim IE As Object
- Sub IE_Application()
- Set IE = CreateObject("InternetExplorer.Application")
- With IE
- .Navigate "http://www.gretai.org.tw/ch/stock/statistics/monthly/st42.php"
- .Visible = True '不顯示ie
- Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
- End With
- End Sub
- Sub 上櫃年成交資訊()
- Dim E As Range, xPath As String, xFile As String, A As Object, fs As Object, F As Object, IE_URL As String
- Dim i As Integer, ii As Integer, t As Date, AR(), Rng As Range, r, C, S
- Set fs = CreateObject("Scripting.FileSystemObject")
- IE_URL = "http://www.gretai.org.tw/ch/stock/statistics/monthly/st42.php"
- t = Time
- Application.DisplayStatusBar = True
- '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
- Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
- If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
- Set Rng = Rng.SpecialCells(xlCellTypeConstants)
- xPath = "D:\財報資料"
- IE_Application '
- Application.StatusBar = " "
- For Each E In Rng
- With IE
- Set A = .Document.getelementbyid("input_stock_code")
- A.Value = E
- A.ParentNode.submit
- Do While .Busy Or .ReadyState <> 4: Loop
- Set A = .Document.getelementsbytagname("TABLE")
- xFile = xPath & "\" & E & "\HPY.txt"
- MkDir_Sub xFile
- With fs.CreateTextFile(xFile, True)
- For i = 1 To A(2).Rows.Length - 1
- S = ""
- For C = 0 To A(2).Rows(i).Cells.Length - 1
- S = S & A(2).Rows(i).Cells(C).innertext & vbTab
- Next
- .WriteLine S
- Next
- .Close
- End With
- ii = ii + 1
- End With
- Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " 共匯入上櫃年成交 " & ii & " 文字檔"
- Next
- IE.Quit
- Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " 共匯入上櫃年成交 " & ii & " 文字檔, 讀取完畢 !! "
- MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - t, ["MM分SS秒"])
- ' ThisWorkbook.Save
- End Sub
- Sub MkDir_Sub(S As String)
- Dim AR, i As Integer, xPath As String
- If Dir(S) = "" Then
- AR = Split(S, "\")
- xPath = AR(0)
- For i = 1 To UBound(AR) - 1
- xPath = xPath & "\" & AR(i)
- If Dir(xPath, vbDirectory) = "" Then MkDir xPath
- Next
- End If
- End Sub
- Sub Maketxt(xF As String, Q As QueryTable) '將匯入資料存入指定的txt
- Dim fs As Object, E As Range, C As Variant
- Set fs = CreateObject("Scripting.FileSystemObject")
- Set fs = fs.CreateTextFile(xF, True) '創見一個檔案,如檔案存在可覆蓋掉
- For Each E In Q.ResultRange.Rows
- C = Application.Transpose(Application.Transpose(E.Value))
- C = Join(C, vbTab)
- fs.WriteLine C
- Next
- fs.Close
- End Sub
複製代碼
程式碼與資料不足TXT.zip (46.93 KB)
|
|