返回列表 上一主題 發帖

[發問] 可否用迴圈或變數匯入大量資料?

回復 49# GBKEE
感謝GBKEE版主,49#程式碼我研究了幾天,不過還是太深奧了,有看沒有很懂,但實際上執行結果是成功的,有試著依樣畫葫蘆,將上市的網頁連結及相關引數帶入程式碼試著擷取資料
但在跑到 .ParentNode.submit會出現沒有使用權限,因為不懂這段程式碼的意思,能請您再幫忙一下嗎?
另外不知道在49#的執行結果中,有沒有辦法將月份越新的往上排序呢?再麻煩您一下囉!感謝!
例如:程式執行結果為
103年1月
103年2月
103年3月
103年4月

希望結果為
103年4月
103年3月
103年2月
103年1月
  1. Option Explicit
  2. Dim IE As Object
  3. Sub IE_Application()
  4.     Set IE = CreateObject("InternetExplorer.Application")
  5.     With IE
  6.         .Navigate "http://www.twse.com.tw/ch/trading/exchange/FMSRFK/FMSRFKMAIN.php"
  7. '        .Visible = True   '不顯示ie
  8.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  9.   End With
  10. End Sub
  11. Sub 上市月成交資訊()
  12.     Dim Rng As Range, Rng1 As Range, E As Range, X As Range, T As Date, xPath As String, xFile As String
  13.     Dim Ea As Variant, ar(), ii As Integer
  14.     T = Time
  15.     Application.DisplayStatusBar = True
  16.     '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  17.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  18.     Set Rng1 = ThisWorkbook.Sheets(3).Range("b:b")
  19.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  20.     If Application.Count(Rng1) = 0 Then MsgBox "沒有股票代號": Exit Sub
  21.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  22.     Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants)
  23.     xPath = "G:\財報資料"
  24.     IE_Application    '
  25.     Application.StatusBar = " "
  26.     For Each E In Rng
  27.         Sheets(1).UsedRange.Clear  '下載資料置於此工作表,變換股票時:清空
  28.         For Each X In Rng1
  29.             With IE
  30.                 .Document.getElementsByTagName("select")("myear").Value = X
  31.                  With .Document.getelementbyid("STK_NO")
  32.                     .Value = E
  33.                     .Document.getelementSbyNAME("login_btn")(0).Click  '按下查詢
  34.                     .ParentNode.submit
  35.                 End With
  36.                 Do While .Busy Or .readyState <> 4:    Loop
  37.                 If Application.Count(Sheets(1).UsedRange) = 0 Then '此工作表清空時:下載第一年度時
  38.                     ar = Array(0, 2)
  39.                 Else
  40.                     ar = Array(2)
  41.                 End If
  42.                 For Each Ea In ar
  43.                     Ep .Document.getElementsByTagName("TABLE")(Ea).outerHTML
  44.                 Next
  45.             ii = ii + 1
  46.             End With
  47.         Next X
  48.         xFile = xPath & "\" & E & "\HPM.txt"
  49.         MkDir_Sub xFile
  50.         Maketxt xFile, Sheets(1).UsedRange
  51.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii & " 文字檔"
  52.     Next E
  53.     IE.Quit
  54.     Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii & " 文字檔,  讀取完畢 !! "
  55.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
  56.     ThisWorkbook.Save
  57. End Sub
  58. Sub Ep(S As String)
  59.     Dim D As New DataObject, E As Shape, FormDLL As String
  60.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  61.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
  62.     On Error GoTo ER
  63.     With D
  64.         .SetText S
  65.         .PutInClipboard
  66.         With Sheets(1)
  67.             .Range("a" & .Rows.Count).End(xlUp).Offset(1).Select
  68.             .PasteSpecial Format:="Unicode 文字"
  69.         End With
  70.     End With
  71.     Exit Sub
  72. ER:
  73.     FormDLL = "FM20.DLL"
  74.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\" & FormDLL
  75.     Resume
  76. End Sub
  77. Sub MkDir_Sub(S As String)
  78.     Dim ar, i As Integer, xPath As String
  79.     If Dir(S) = "" Then
  80.         ar = Split(S, "\")
  81.         xPath = ar(0)
  82.         For i = 1 To UBound(ar) - 1
  83.             xPath = xPath & "\" & ar(i)
  84.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  85.         Next
  86.     End If
  87. End Sub
  88. Sub Maketxt(xF As String, Q As Range)    '將匯入資料存入指定的txt
  89.     Dim fs As Object, E As Range, C As Variant
  90.     Q.Range("C1") = ""
  91.     Q.Range("a4", Q.Range("a4").End(xlDown)).Replace "年", ""
  92.     Q.Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  93.     Set fs = CreateObject("Scripting.FileSystemObject")
  94.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  95.     For Each E In Q.Rows
  96.         C = Application.Transpose(Application.Transpose(E.Value))
  97.         C = Join(C, vbTab)
  98.         fs.WriteLine C
  99.     Next
  100.     fs.Close
  101. End Sub
複製代碼
上市.zip (20.75 KB)

TOP

回復 51# GBKEE
感謝GBKEE版主,這些程式碼又讓我學到很多,我也試著將上櫃資料自行排序,試了許久,終於成功了
不過這個上市在寫入文字檔的程式碼中有一些不太了解,想再向您請益:
1.和市櫃不同的地方是多了一個Code,而這個Code會直接代入E的代號,想請問這個Code是什麼?
2.在 [Q.Cells(1) = Code & " 月成交資訊"   '加入股票代號]這段程式碼的結果會變成[股票代號&月成交資訊],因為我想除了連股票代號之外,連股票名稱也能顯示
舉例來說:跑第一檔股票時結果會是"1101月成交資訊",我想要的結果是"1101亞泥月成交資訊",我有試著用原來A1儲存格保留的"103年1101亞泥月成交資訊"
來做修改,有用mid函數,也有用replce,並將[Q.Cells(1) = Code & " 月成交資訊"   '加入股票代號]用註解跳過,但只要這段跳過就會在fs.WriteLine C出錯
如果不跳過,在這裡加入其他程式碼一樣會在fs.WriteLine C出錯,不清楚為何會這樣?

原來的程式碼
  1. Sub Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  2.     Dim fs As Object, E As Range, C As Variant
  3.     Set fs = CreateObject("Scripting.FileSystemObject")
  4.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  5.     Q.Cells(1) = Code & " 月成交資訊"   '加入股票代號
  6.     For Each E In Q.Rows
  7.         C = Application.Transpose(Application.Transpose(E.Value))
  8.         C = Join(C, vbTab)
  9.         fs.WriteLine C
  10.     Next
  11.     fs.Close
  12. End Sub
複製代碼
我自己修改的程式碼(會出錯)
  1. Sub Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  2.     Dim fs As Object, E As Range, C As Variant, A As String, B As String
  3.     Set fs = CreateObject("Scripting.FileSystemObject")
  4.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  5.     A = Q.Cells(1)
  6.     B = Mid(A, 5, 15)
  7.     Q.Cells(1) = B
  8. '    Q.Cells(1) = Code & " 月成交資訊"   '加入股票代號
  9.     For Each E In Q.Rows
  10.         C = Application.Transpose(Application.Transpose(E.Value))
  11.         C = Join(C, vbTab)
  12.         fs.WriteLine C
  13.     Next
  14.     fs.Close
  15. End Sub
複製代碼

TOP

回復 53# GBKEE
版主您好,不好意思,請問一下加了a=Q.Cells(1)有什麼不同呢?雖然執行時不會出錯,不過結果還是沒變,因為執行到下面,輸出的結果還是Q.Cells(1) = Code & "月成交資訊"
我試著再修改了一下,不過還是如之前會出錯,不知您說的變通是指?????
  1. Sub Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  2.     Dim fs As Object, E As Range, C As Variant
  3.     Dim A As String, B As String
  4.     A = Q.Cells(1)
  5. '    B = Mid(A, 9, 20)  '這段程式碼加入的話就會出錯,用註解跳過則不會出錯,但這段程式碼是要擷取正確文字
  6.     Set fs = CreateObject("Scripting.FileSystemObject")
  7.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  8.     Q.Cells(1) = Code & B   '加入股票代號
  9.     For Each E In Q.Rows
  10.         C = Application.Transpose(Application.Transpose(E.Value))
  11.         C = Join(C, vbTab)
  12.         fs.WriteLine C
  13.     Next
  14.     fs.Close
  15. End Sub
複製代碼

TOP

回復 51# GBKEE
不好意思,GBKEE版主,又發現一個比較大的問題了,我想要的資料是近三年的月成交資料,可是有些個股是近1-2年才上市櫃的,所以在上市網頁輸入時會出現查無
而在上櫃網頁則是只會出現上櫃後開始的月成交資料,當查到上市櫃前的年份就會出現查無,如果讓VBA直接執行的話,執行到上市櫃不到三年的個股代號就會出錯
還會有無窮迴圈的狀況,不知道有沒有辦法加入其他程式碼來避免這種錯誤?
舉例來說:如果個股只有上市櫃不到一年、或一年到兩年,能不能只擷取該個股有的資料,當遇到查無的年份就結束該個股的資料擷取,跳到下一個股
附件是我將Sheet(3)代入一些上市櫃不到三年的個股代號會出錯的EXCEL,再請您指導一下,謝謝!

上市櫃月成交資訊.zip (37.25 KB)

TOP

回復 53# GBKEE
GBKEE版主您好,這幾天終於弄懂了為什麼這個程式碼會出錯了,原來您說的不可見字元是Q.Cells(1)文字中內含的問號
我是著用以下方法讓它不會出錯,也能取得個股名稱一起代入,不過下面的方法只能適用個股名稱是兩個字的,如果遇上三個字的還是會出錯
不知是否還有其他變通方式?再請您指點一下,謝謝!
另外還想請問您Code As String的Code是怎麼帶出個股編號的?因為我在這個程式中似乎找不到和Code有相關的程式碼能取得個股編號,再麻煩您解惑一下,謝謝!
  1. Sub Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  2.     Dim fs As Object, E As Range, C As Variant, A As String, B As String
  3.     Set fs = CreateObject("Scripting.FileSystemObject")
  4.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  5.     A = Q.Cells(1)
  6.     B = Mid(A, 11, 2)
  7.     Q.Cells(1) = Code & B & " 月成交資訊"   '加入股票代號
  8.     For Each E In Q.Rows
  9.         C = Application.Transpose(Application.Transpose(E.Value))
  10.         C = Join(C, vbTab)
  11.         fs.WriteLine C
  12.     Next
  13.     fs.Close
  14. End Sub
複製代碼

TOP

回復 53# GBKEE
抱歉,更正一下,上一篇回覆的內容有點錯誤
如果遇上三個字的還是會出錯改成如果遇上三個字的個股名稱就只能擷取到兩個字

TOP

回復 40# GBKEE
抱歉,先前有個問題一直忘了問您,您在40#中回覆的程式碼確實是可以製圖及取得集保戶近一年資料,不過和我想要的格式不大相同
在您後面指導如何利用檢視網頁原始碼取得關鍵引數後,我有試著利用取得上市月資料的程式碼將集保戶網頁及相關引數代入,但在中途就會出錯
不大清楚問題出在什麼地方,附上程式碼以及我想要完成的文字檔格式,再麻煩您教導一下,感謝!

集保戶網頁

  1. Option Explicit
  2. Dim IE As Object
  3. Sub IE_Application()
  4.     Set IE = CreateObject("InternetExplorer.Application")
  5.     With IE
  6.         .Navigate "http://www.tdcc.com.tw/smWeb/QryStock.jsp"
  7.         .Visible = True   '不顯示ie
  8.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  9.   End With
  10. End Sub
  11. Sub 集保()
  12.     Dim Rng As Range, Rng1 As Range, E As Range, X As Range, T As Date, xPath As String, xFile As String
  13.     Dim Ea As Variant, ar(), ii As Integer
  14.     T = Time
  15.     Application.DisplayStatusBar = True
  16.     '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  17.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  18.     Set Rng1 = ThisWorkbook.Sheets(3).Range("b:b")
  19.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  20.     If Application.Count(Rng1) = 0 Then MsgBox "沒有股票代號": Exit Sub
  21.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  22.     Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants)
  23.     xPath = "D:\財報資料"
  24.     IE_Application    '
  25.     Application.StatusBar = " "
  26.     For Each E In Rng
  27.         With Sheets(1)
  28.             .Activate
  29.             .Cells.Clear  '下載資料置於此工作表,變換股票時:清空
  30.         End With
  31.         For Each X In Rng1
  32.             With IE
  33.                 .Document.getElementsByTagName("select")("SCA_DATE").Value = X
  34.                  With .Document.getelementbyid("StockNo")
  35.                     .Value = E
  36.                     .Document.getelementSbyNAME("login_btn")(0).Click  '按下查詢
  37.                 End With
  38.                 Do While .Busy Or .readyState <> 4:    Loop
  39.                 If .Document.getElementsByTagName("TABLE")(7).Rows.Length > 1 Then
  40.                     Ep .Document.getElementsByTagName("TABLE")(7).outerHTML
  41.                 Else
  42.                     GoTo Nn
  43.                 End If
  44.             End With
  45.         Next X
  46. Nn:
  47.         xFile = xPath & "\" & E & "\SHD.txt"
  48.         MkDir_Sub xFile
  49.         Maketxt xFile, Sheets(1).Range("A1").CurrentRegion, E.Value
  50.         ii = ii + 1
  51.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上市月成交 " & ii & " 文字檔"
  52.     Next E
  53.     IE.Quit
  54.     Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上市月成交 " & ii & " 文字檔,  讀取完畢 !! "
  55.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
  56. '    ThisWorkbook.Save
  57. End Sub
  58. Sub Ep(S As String)
  59.     Dim D As New DataObject, i As Integer, FormDLL As String, Rng As Range
  60.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  61.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
  62.     On Error GoTo ER
  63.     With D
  64.         .SetText S
  65.         .PutInClipboard
  66.         With Sheets(1)
  67.             With .Range("a" & .Rows.Count).End(xlUp)
  68.                 If .Row = 1 Then
  69.                     Set Rng = .Cells
  70.                 Else
  71.                     Set Rng = .Offset(1)
  72.                 End If
  73.                 Rng.Select
  74.                 .Parent.PasteSpecial Format:="Unicode 文字"
  75.                 Set Rng = Rng.Range("A3", Rng.Range("A3").End(xlDown)).Resize(, 9)
  76.                 'Sort :資料排序
  77.                 Rng.Sort Key1:=Rng.Range("B2"), Order1:=xlDescending, Header:=xlYes ', _
  78.                 OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
  79.                 :=xlStroke, DataOption1:=xlSortNorma
  80.                 If .Row = 1 Then
  81.                     .Range("A2").EntireRow.Delete
  82.                 Else
  83.                     .Range("A2:A4").EntireRow.Delete
  84.                 End If
  85.             End With
  86.         End With
  87.     End With
  88.     Exit Sub
  89. ER:
  90.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\FM20.DLL"
  91.     Resume
  92. End Sub
  93. Sub Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  94.     Dim fs As Object, E As Range, C As Variant
  95.     Set fs = CreateObject("Scripting.FileSystemObject")
  96.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  97.     For Each E In Q.Rows
  98.         C = Application.Transpose(Application.Transpose(E.Value))
  99.         C = Join(C, vbTab)
  100.         fs.WriteLine C
  101.     Next
  102.     fs.Close
  103. End Sub
  104. Sub MkDir_Sub(S As String)
  105.     Dim ar, i As Integer, xPath As String
  106.     If Dir(S) = "" Then
  107.         ar = Split(S, "\")
  108.         xPath = ar(0)
  109.         For i = 1 To UBound(ar) - 1
  110.             xPath = xPath & "\" & ar(i)
  111.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  112.         Next
  113.     End If
  114. End Sub
複製代碼
集保戶+文字檔.zip (24.3 KB)

TOP

回復 60# GBKEE
版主抱歉,請問一下,我將您提供的程式碼代入後出錯位置及出錯訊息仍和之前一樣,能麻煩您再幫忙看一下嗎?感恩!
集保最新.zip (27.46 KB)
  1. Option Explicit
  2. Dim IE As Object, A As Integer
  3. Sub IE_Application()
  4.     Dim I As Integer
  5.     Set IE = CreateObject("InternetExplorer.Application")
  6.     With IE
  7.         .Navigate "http://www.tdcc.com.tw/smWeb/QryStock.jsp"
  8.         .Visible = True   '不顯示ie
  9.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  10.         '讀取集保戶股權分散表查詢的資料日期總個數
  11.         A = .document.getelementsByTagName("select")("SCA_DATE").Length - 1
  12.   End With
  13. End Sub
  14. Sub 集保()
  15.     Dim Rng As Range, E As Range, x As Variant, T As Date, xPath As String, xFile As String
  16.     Dim Ea As Variant, ii As Integer
  17.     T = Time
  18.     Application.DisplayStatusBar = True
  19.     '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  20.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  21.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  22.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  23.     xPath = "D:\財報資料"
  24.     IE_Application    '
  25.     Application.StatusBar = " "
  26.     For Each E In Rng
  27.         With Sheets(1)
  28.             .Activate
  29.             .Cells.Clear  '下載資料置於此工作表,變換股票時:清空
  30.         End With
  31.         For x = A - 1 To A
  32.             With IE
  33.                 .document.getelementsByTagName("select")("SCA_DATE")(x).Selected = True
  34.                                             '這裡的資料日期 需用 Select 它是有[年度月份日期]的
  35.                  .document.getElementById("StockNo").Value = E
  36.                     .document.getelementsByTagName("INPUT")("sub").Click  '按下查詢
  37.                        '這裡的查詢是<INPUT TYPE="submit" VALUE="查詢" name="sub">
  38.                 'End With
  39.                 Do While .Busy Or .readyState <> 4:    Loop
  40.                 Ep .document.getelementsByTagName("TABLE")(7).outerHTML
  41.             End With
  42.         Next x
  43.         xFile = xPath & "\" & E & "\SHD.txt"
  44.         MkDir_Sub xFile
  45.         
  46.         Maketxt xFile, Sheets(1).Range("A1").CurrentRegion, E.Value
  47.         '***想請問您Code As String的Code是怎麼帶出個股編號的 ****
  48.         'xFile(第一個引數), Sheets(1).Range("A1").CurrentRegion(第二個引數),E.Value(第三個引數)
  49.         
  50.         ii = ii + 1
  51.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上市月成交 " & ii & " 文字檔"
  52.     Next E
  53.     IE.Quit
  54.     Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上市月成交 " & ii & " 文字檔,  讀取完畢 !! "
  55.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
  56. '    ThisWorkbook.Save
  57. End Sub
  58. Sub Ep(S As String)
  59.     Dim D As New DataObject, E As Shape, FormDLL As String, Rng As Range
  60.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  61.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
  62.     On Error GoTo ER
  63.     With D
  64.         .SetText S
  65.         .PutInClipboard
  66.         With Sheets(1)
  67.             .Range("a" & .Rows.Count).End(xlUp).Offset(1).Select
  68.             .PasteSpecial Format:="Unicode 文字"
  69. '            Set Rng = Selection
  70. '                Rng.Sort Key1:=Rng.Range("B2"), Order1:=xlDescending, Header:=xlYes ', _
  71.                 OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
  72.                 :=xlStroke, DataOption1:=xlSortNorma
  73.         End With
  74.     End With
  75.     Exit Sub
  76. ER:
  77.     FormDLL = "FM20.DLL"
  78.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\" & FormDLL
  79.     Resume
  80. End Sub
  81. Sub Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  82.           '***想請問您Code As String的Code是怎麼帶出個股編號的 ****
  83.           ' xF(接收的引數名稱) As String(字串型態), Q As Range(Range物件型態), Code As String(字串型態)
  84.     Dim fs As Object, E As Range, C As Variant
  85.     Set fs = CreateObject("Scripting.FileSystemObject")
  86.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  87.     For Each E In Q.Rows
  88.         C = Application.Transpose(Application.Transpose(E.Value))
  89.         C = Join(C, vbTab)
  90.         fs.WriteLine C
  91.     Next
  92.     fs.Close
  93. End Sub
  94. Sub MkDir_Sub(S As String)
  95.     Dim AR, I As Integer, xPath As String
  96.     If Dir(S) = "" Then
  97.         AR = Split(S, "\")
  98.         xPath = AR(0)
  99.         For I = 1 To UBound(AR) - 1
  100.             xPath = xPath & "\" & AR(I)
  101.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  102.         Next
  103.     End If
  104. End Sub
複製代碼

TOP

回復 62# GBKEE
版主您好,不好意思,我有試著將For x = A - 1 To A 改成For x = 0 To A,不過出錯訊息和出錯位置仍相同,能不能再麻煩您測試一下呢?
另外在更之前的上櫃年成交資料,就是用比較舊的寫法,不貼上EXCEL直接寫入TXT的程式碼,雖然有資料,不過最上方少了個股代號和名稱,不知這部分能不能
也擷取到資料並寫入TXT?或是是有可以先貼到EXCEL再寫入TXT的方法?再麻煩您了!
  1. Option Explicit
  2. Dim IE As Object, A As Integer
  3. Sub IE_Application()
  4.     Dim I As Integer
  5.     Set IE = CreateObject("InternetExplorer.Application")
  6.     With IE
  7.         .Navigate "http://www.tdcc.com.tw/smWeb/QryStock.jsp"
  8.         .Visible = True   '不顯示ie
  9.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  10.         '讀取集保戶股權分散表查詢的資料日期總個數
  11.         A = .document.getelementsByTagName("select")("SCA_DATE").Length - 1
  12.   End With
  13. End Sub
  14. Sub 集保()
  15.     Dim Rng As Range, E As Range, x As Variant, T As Date, xPath As String, xFile As String
  16.     Dim Ea As Variant, ii As Integer
  17.     T = Time
  18.     Application.DisplayStatusBar = True
  19.     '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  20.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  21.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  22.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  23.     xPath = "D:\財報資料"
  24.     IE_Application    '
  25.     Application.StatusBar = " "
  26.     For Each E In Rng
  27.         With Sheets(1)
  28.             .Activate
  29.             .Cells.Clear  '下載資料置於此工作表,變換股票時:清空
  30.         End With
  31.         For x = 0 To A
  32.             With IE
  33.                 .document.getelementsByTagName("select")("SCA_DATE")(x).Selected = True
  34.                                             '這裡的資料日期 需用 Select 它是有[年度月份日期]的
  35.                  .document.getElementById("StockNo").Value = E
  36.                     .document.getelementsByTagName("INPUT")("sub").Click  '按下查詢
  37.                        '這裡的查詢是<INPUT TYPE="submit" VALUE="查詢" name="sub">
  38.                 'End With
  39.                 Do While .Busy Or .readyState <> 4:    Loop
  40.                 Ep .document.getelementsByTagName("TABLE")(7).outerHTML
  41.             End With
  42.         Next x
  43.         xFile = xPath & "\" & E & "\SHD.txt"
  44.         MkDir_Sub xFile
  45.         
  46.         Maketxt xFile, Sheets(1).Range("A1").CurrentRegion, E.Value
  47.         '***想請問您Code As String的Code是怎麼帶出個股編號的 ****
  48.         'xFile(第一個引數), Sheets(1).Range("A1").CurrentRegion(第二個引數),E.Value(第三個引數)
  49.         
  50.         ii = ii + 1
  51.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上市月成交 " & ii & " 文字檔"
  52.     Next E
  53.     IE.Quit
  54.     Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上市月成交 " & ii & " 文字檔,  讀取完畢 !! "
  55.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
  56. '    ThisWorkbook.Save
  57. End Sub
  58. Sub Ep(S As String)
  59.     Dim D As New DataObject, E As Shape, FormDLL As String, Rng As Range
  60.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  61.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
  62.     On Error GoTo ER
  63.     With D
  64.         .SetText S
  65.         .PutInClipboard
  66.         With Sheets(1)
  67.             .Range("a" & .Rows.Count).End(xlUp).Offset(1).Select
  68.             .PasteSpecial Format:="Unicode 文字"
  69. '            Set Rng = Selection
  70. '                Rng.Sort Key1:=Rng.Range("B2"), Order1:=xlDescending, Header:=xlYes ', _
  71.                 OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
  72.                 :=xlStroke, DataOption1:=xlSortNorma
  73.         End With
  74.     End With
  75.     Exit Sub
  76. ER:
  77.     FormDLL = "FM20.DLL"
  78.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\" & FormDLL
  79.     Resume
  80. End Sub
  81. Sub Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  82.           '***想請問您Code As String的Code是怎麼帶出個股編號的 ****
  83.           ' xF(接收的引數名稱) As String(字串型態), Q As Range(Range物件型態), Code As String(字串型態)
  84.     Dim fs As Object, E As Range, C As Variant
  85.     Set fs = CreateObject("Scripting.FileSystemObject")
  86.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  87.     For Each E In Q.Rows
  88.         C = Application.Transpose(Application.Transpose(E.Value))
  89.         C = Join(C, vbTab)
  90.         fs.WriteLine C
  91.     Next
  92.     fs.Close
  93. End Sub
  94. Sub MkDir_Sub(S As String)
  95.     Dim AR, I As Integer, xPath As String
  96.     If Dir(S) = "" Then
  97.         AR = Split(S, "\")
  98.         xPath = AR(0)
  99.         For I = 1 To UBound(AR) - 1
  100.             xPath = xPath & "\" & AR(I)
  101.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  102.         Next
  103.     End If
  104. End Sub
複製代碼
  1. Option Explicit
  2. Dim IE As Object
  3. Sub IE_Application()
  4.     Set IE = CreateObject("InternetExplorer.Application")
  5.     With IE
  6.         .Navigate "http://www.gretai.org.tw/ch/stock/statistics/monthly/st42.php"
  7.         .Visible = True   '不顯示ie
  8.         Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
  9.   End With
  10. End Sub
  11. Sub 上櫃年成交資訊()
  12.     Dim E As Range, xPath As String, xFile As String, A As Object, fs As Object, F As Object, IE_URL As String
  13.     Dim i As Integer, ii As Integer, t As Date, AR(), Rng As Range, r, C, S
  14.     Set fs = CreateObject("Scripting.FileSystemObject")
  15.     IE_URL = "http://www.gretai.org.tw/ch/stock/statistics/monthly/st42.php"
  16.     t = Time
  17.     Application.DisplayStatusBar = True
  18.     '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  19.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  20.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  21.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  22.     xPath = "D:\財報資料"
  23.     IE_Application    '
  24.     Application.StatusBar = " "
  25.     For Each E In Rng
  26.         With IE
  27.             Set A = .Document.getelementbyid("input_stock_code")
  28.             A.Value = E
  29.             A.ParentNode.submit
  30.             Do While .Busy Or .ReadyState <> 4:    Loop
  31.             Set A = .Document.getelementsbytagname("TABLE")
  32.             xFile = xPath & "\" & E & "\HPY.txt"
  33.             MkDir_Sub xFile
  34.             With fs.CreateTextFile(xFile, True)
  35.                 For i = 1 To A(2).Rows.Length - 1
  36.                     S = ""
  37.                     For C = 0 To A(2).Rows(i).Cells.Length - 1
  38.                         S = S & A(2).Rows(i).Cells(C).innertext & vbTab
  39.                     Next
  40.                     .WriteLine S
  41.                 Next
  42.                 .Close
  43.             End With
  44.             ii = ii + 1
  45.         End With
  46.         Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " 共匯入上櫃年成交 " & ii & " 文字檔"
  47.     Next
  48.     IE.Quit
  49.     Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " 共匯入上櫃年成交 " & ii & " 文字檔,  讀取完畢 !! "
  50.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - t, ["MM分SS秒"])
  51. '    ThisWorkbook.Save
  52. End Sub
  53. Sub MkDir_Sub(S As String)
  54.     Dim AR, i As Integer, xPath As String
  55.     If Dir(S) = "" Then
  56.         AR = Split(S, "\")
  57.         xPath = AR(0)
  58.         For i = 1 To UBound(AR) - 1
  59.             xPath = xPath & "\" & AR(i)
  60.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  61.         Next
  62.     End If
  63. End Sub
  64. Sub Maketxt(xF As String, Q As QueryTable)   '將匯入資料存入指定的txt
  65.     Dim fs As Object, E As Range, C As Variant
  66.     Set fs = CreateObject("Scripting.FileSystemObject")
  67.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  68.     For Each E In Q.ResultRange.Rows
  69.         C = Application.Transpose(Application.Transpose(E.Value))
  70.         C = Join(C, vbTab)
  71.         fs.WriteLine C
  72.     Next
  73.     fs.Close
  74. End Sub
複製代碼
程式碼與資料不足TXT.zip (46.93 KB)

TOP

回復 64# GBKEE
版主您好,集保程序我有試著再執行一次,結果還是一樣,出錯訊息如圖:


另外您提供的程式碼我有加入上櫃年成交中,最上方是有寫入了,不過是寫入空白資料,不是股票代號和名稱,能麻煩您再看一下嗎?
  1. Sub 上櫃年成交資訊()
  2.     Dim E As Range, xPath As String, xFile As String, A As Object, fs As Object, F As Object, IE_URL As String
  3.     Dim i As Integer, ii As Integer, t As Date, AR(), Rng As Range, r, C, S
  4.     Set fs = CreateObject("Scripting.FileSystemObject")
  5.     IE_URL = "http://www.gretai.org.tw/ch/stock/statistics/monthly/st42.php"
  6.     t = Time
  7.     Application.DisplayStatusBar = True
  8.     '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  9.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  10.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  11.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  12.     xPath = "D:\財報資料"
  13.     IE_Application    '
  14.     Application.StatusBar = " "
  15.     For Each E In Rng
  16.         With IE
  17.             Set A = .Document.getelementbyid("input_stock_code")
  18.             A.Value = E
  19.             A.ParentNode.submit
  20.             Do While .Busy Or .ReadyState <> 4:    Loop
  21.             Set A = .Document.getelementsbytagname("TABLE")
  22.             xFile = xPath & "\" & E & "\HPY.txt"
  23.             MkDir_Sub xFile
  24.             With fs.CreateTextFile(xFile, True)
  25.                 S = Split(A(0).innertext, ")")(1)
  26.                 .WriteLine Split(S, vbLf)(0) '最上方加上個股代號和名稱了.
  27.                 For i = 1 To A(2).Rows.Length - 1
  28.                     S = ""
  29.                     For C = 0 To A(2).Rows(i).Cells.Length - 1
  30.                         S = S & A(2).Rows(i).Cells(C).innertext & vbTab
  31.                     Next
  32.                     .WriteLine S
  33.                 Next
  34.                 .Close
  35.             End With
  36.             ii = ii + 1
  37.         End With
  38.         Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " 共匯入上櫃年成交 " & ii & " 文字檔"
  39.     Next
  40.     IE.Quit
  41.     Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " 共匯入上櫃年成交 " & ii & " 文字檔,  讀取完畢 !! "
  42.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - t, ["MM分SS秒"])
  43. '    ThisWorkbook.Save
  44. End Sub
複製代碼

TOP

        靜思自在 : 人要知福、惜福、再造福。
返回列表 上一主題