返回列表 上一主題 發帖

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

回復 50# smart3135


   
但在跑到 .ParentNode.submit會出現沒有使用權限,因為不懂這段程式碼的意思,能請您再幫忙一下嗎?
希望結果為
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 = "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")("myear").Value = X
  34.                  With .Document.getelementbyid("STK_NO")
  35.                     .Value = E
  36.                     .Document.getelementSbyNAME("login_btn")(0).Click  '按下查詢
  37.                 End With
  38.                 Do While .Busy Or .readyState <> 4:    Loop
  39.                 Ep .Document.getElementsByTagName("TABLE")(7).outerHTML
  40.             End With
  41.         Next X
  42.         xFile = xPath & "\" & E & "\HPM.txt"
  43.         MkDir_Sub xFile
  44.         Maketxt xFile, Sheets(1).Range("A1").CurrentRegion, E.Value
  45.         ii = ii + 1
  46.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii & " 文字檔"
  47.     Next E
  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 Ep(S As String)
  54.     Dim D As New DataObject, i As Integer, FormDLL As String, Rng As Range
  55.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  56.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
  57.     On Error GoTo ER
  58.     With D
  59.         .SetText S
  60.         .PutInClipboard
  61.         With Sheets(1)
  62.             With .Range("a" & .Rows.Count).End(xlUp)
  63.                 If .Row = 1 Then
  64.                     Set Rng = .Cells
  65.                 Else
  66.                     Set Rng = .Offset(1)
  67.                 End If
  68.                 Rng.Select
  69.                 .Parent.PasteSpecial Format:="Unicode 文字"
  70.                 Set Rng = Rng.Range("A3", Rng.Range("A3").End(xlDown)).Resize(, 9)
  71.                 'Sort :資料排序
  72.                 Rng.Sort Key1:=Rng.Range("B2"), Order1:=xlDescending, Header:=xlYes ', _
  73.                 OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
  74.                 :=xlStroke, DataOption1:=xlSortNorma
  75.                 If .Row = 1 Then
  76.                     .Range("A2").EntireRow.Delete
  77.                 Else
  78.                     .Range("A2:A4").EntireRow.Delete
  79.                 End If
  80.             End With
  81.         End With
  82.     End With
  83.     Exit Sub
  84. ER:
  85.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\FM20.DLL"
  86.     Resume
  87. End Sub
  88. Sub Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  89.     Dim fs As Object, E As Range, C As Variant
  90.     Set fs = CreateObject("Scripting.FileSystemObject")
  91.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  92.     Q.Cells(1) = Code & " 月成交資訊"   '加入股票代號
  93.     For Each E In Q.Rows
  94.         C = Application.Transpose(Application.Transpose(E.Value))
  95.         C = Join(C, vbTab)
  96.         fs.WriteLine C
  97.     Next
  98.     fs.Close
  99. End Sub
  100. Sub MkDir_Sub(S As String)
  101.     Dim ar, i As Integer, xPath As String
  102.     If Dir(S) = "" Then
  103.         ar = Split(S, "\")
  104.         xPath = ar(0)
  105.         For i = 1 To UBound(ar) - 1
  106.             xPath = xPath & "\" & ar(i)
  107.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  108.         Next
  109.     End If
  110. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

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

回復 52# smart3135
Q.Cells(1) 有不可見字元如圖,會造成程式碼的錯誤,所以變通一下
  1. Q.Cells(1) = Code & " 月成交資訊"   '加入股票代號
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

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

回復 55# smart3135


   
加了a=Q.Cells(1)有什麼不同呢?雖然執行時不會出錯,不過結果還是沒變,

加了a=Q.Cells(1),只是為了顯示於圖示:區域變數視窗中的不可見字元
  1. Sub 上市月成交資訊()
  2.     Dim Rng As Range, Rng1 As Range, E As Range, X As Range, T As Date, xPath As String, xFile As String
  3.     Dim Ea As Variant, ar(), ii As Integer
  4.     T = Time
  5.     Application.DisplayStatusBar = True
  6.     '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  7.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  8.     Set Rng1 = ThisWorkbook.Sheets(3).Range("b:b")
  9.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  10.     If Application.Count(Rng1) = 0 Then MsgBox "沒有股票代號": Exit Sub
  11.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  12.     Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants)
  13.     xPath = "D:\財報資料"
  14.     IE_Application    '
  15.     Application.StatusBar = " "
  16.     For Each E In Rng
  17.         With Sheets(1)
  18.             .Activate
  19.             .Cells.Clear  '下載資料置於此工作表,變換股票時:清空
  20.         End With
  21.         For Each X In Rng1
  22.             With IE
  23.                 .Document.getElementsByTagName("select")("myear").Value = X
  24.                  With .Document.getelementbyid("STK_NO")
  25.                     .Value = E
  26.                     .Document.getelementSbyNAME("login_btn")(0).Click  '按下查詢
  27.                 End With
  28.                 Do While .Busy Or .readyState <> 4:    Loop
  29.                 If .Document.getElementsByTagName("TABLE")(7).Rows.Length > 1 Then
  30.                     Ep .Document.getElementsByTagName("TABLE")(7).outerHTML
  31.                 Else
  32.                     GoTo Nn
  33.                 End If
  34.             End With
  35.         Next X
  36. Nn:
  37.         xFile = xPath & "\" & E & "\HPM.txt"
  38.         MkDir_Sub xFile
  39.         Maketxt xFile, Sheets(1).Range("A1").CurrentRegion, E.Value
  40.         ii = ii + 1
  41.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii & " 文字檔"
  42.     Next E
  43.     IE.Quit
  44.     Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii & " 文字檔,  讀取完畢 !! "
  45.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
  46. '    ThisWorkbook.Save
  47. End Sub
複製代碼
  1. Option Explicit
  2. Dim IE As Object
  3. Sub IE_Application()
  4.     If Not IE Is Nothing Then IE.Quit  '當查到上櫃的年份就會出現查無,此IE無法再度查詢,關閉它
  5.     Set IE = CreateObject("InternetExplorer.Application")
  6.     With IE
  7.         .Navigate "http://www.gretai.org.tw/ch/stock/statistics/monthly/st44.php"
  8.         .Visible = True   '不顯示ie
  9.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  10.     End With
  11. End Sub
  12. Sub 上櫃月成交資訊()
  13.     Dim Rng As Range, Rng1 As Range, E As Range, X As Range, T As Date, xPath As String, xFile As String
  14.     Dim Ea As Variant, AR(), ii As Integer, Msg As Boolean
  15.     T = Time
  16.     Application.DisplayStatusBar = True
  17.     '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  18.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  19.     Set Rng1 = ThisWorkbook.Sheets(3).Range("b:b")
  20.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  21.     If Application.Count(Rng1) = 0 Then MsgBox "沒有股票代號": Exit Sub
  22.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  23.     Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants)
  24.     xPath = "D:\財報資料"
  25.     Application.StatusBar = " "
  26.     For Each E In Rng
  27.         If Msg = False Then IE_Application   '當查到上櫃的年份就會出現查無,重開IE
  28.         Sheets(1).UsedRange.Clear            '下載資料置於此工作表,變換股票時:清空
  29.         For Each X In Rng1
  30.             With IE
  31.                 .document.getElementsByTagName("select")("yy").Value = X
  32.                 Do While .Busy Or .readyState <> 4:    Loop
  33.                  With .document.getelementbyid("input_stock_code")
  34.                     .Value = E
  35.                     .ParentNode.submit
  36.                 End With
  37.                 Do While .Busy Or .readyState <> 4:    Loop
  38.                 If InStr(.document.getElementsByTagName("TABLE")(0).innerHTML, "查無該筆資料") = 0 Then
  39.                     Msg = True
  40.                     If Application.Count(Sheets(1).UsedRange) = 0 Then '此工作表清空時:下載第一年度時
  41.                         AR = Array(0, 2)
  42.                     Else
  43.                         AR = Array(2)
  44.                     End If
  45.                     For Each Ea In AR
  46.                         Ep .document.getElementsByTagName("TABLE")(Ea).outerHTML
  47.                     Next
  48.                 Else
  49.                     Msg = False          '上櫃的年份就會出現查無
  50.                     GoTo NN
  51.                 End If
  52.             ii = ii + 1
  53.             End With
  54.         Next X
  55. NN:
  56.         xFile = xPath & "\" & E & "\HPM.txt"
  57.         MkDir_Sub xFile
  58.         Maketxt xFile, Sheets(1).UsedRange
  59.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii / 3 & " 文字檔"
  60.     Next E
  61.     IE.Quit
  62.     Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii / 3 & " 文字檔,  讀取完畢 !! "
  63.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
  64. End Sub
  65. Sub Maketxt(xF As String, Q As Range)    '將匯入資料存入指定的txt
  66.     Dim fs As Object, E As Range, C As Variant
  67.     Q.Range("C1") = ""
  68.     Q.Range("A1") = Q.Range("B1") & " " & "月成交資料"
  69.     Q.Range("B1") = ""
  70.     Q.Range("a4", Q.Range("a4").End(xlDown)).Replace "年", ""
  71.     Q.Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  72.     Q.Rows(3).Delete     '上櫃月成交資料當月還未結束時就會有資料了,所以要將還沒結束的月份刪除
  73.     '是Q.Rows(3)不 Rows(4)
  74.     Set fs = CreateObject("Scripting.FileSystemObject")
  75.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  76.     For Each E In Q.Rows
  77.         C = Application.Transpose(Application.Transpose(E.Value))
  78.         C = Join(C, vbTab)
  79.         fs.WriteLine C
  80.     Next
  81.     fs.Close
  82. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

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

本帖最後由 GBKEE 於 2014-5-20 16:04 編輯

回復 59# smart3135
  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 Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  59.           '***想請問您Code As String的Code是怎麼帶出個股編號的 ****
  60.           ' xF(接收的引數名稱) As String(字串型態), Q As Range(Range物件型態), Code As String(字串型態)
  61.     Dim fs As Object, E As Range, C As Variant
  62.     Set fs = CreateObject("Scripting.FileSystemObject")
  63.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  64.     For Each E In Q.Rows
  65.         C = Application.Transpose(Application.Transpose(E.Value))
  66.         C = Join(C, vbTab)
  67.         fs.WriteLine C
  68.     Next
  69.     fs.Close
  70. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 人事的艱難與琢磨,就是一種考驗。
返回列表 上一主題