返回列表 上一主題 發帖

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

回復 40# GBKEE
版主您好,剛剛將電腦關閉後重新開啟後再重新執行VBA,結果很不錯,一個平均一秒多,可能之前我在執行VBA時開啟太多其他軟體了

這個是擷取年成交資訊,還有一個月成交資訊,因為月成交資訊是要點月份來選擇,我會先自己做看看,不行的話會再來向您討教
另外您附上的集保資料我還沒試,晚上會利用時間來試試,不過我有稍微瞄了一下,有看到INPUT BOX,因為我是要用迴圈連續擷取資料
不是要用INPUT BOX輸入代碼來擷取資料,也不需要附檔的圖,只需要文字檔內容即,不知道我是否解讀錯誤,會利用時間了解看看,再次感謝您!

TOP

回復 38# GBKEE
GBKEE版主早安,我在抓上市個股年成交資訊又遇到問題了,原本是想利用您在37#的抓取上櫃年成交的程式碼來修改,但卻出錯,能請您再幫忙看一下嗎?
另外因為希望將匯入資料的EXCEL表格做一些修改再存入txt,所以我也有用先前抓其他資料的VBA來測試,但因為先前連結的網站是國泰證券,需要的資料只要將
最後的個股代碼設定成迴圈變數,就能將資料一個一個擷取下來,但上市年成交資訊的網頁不是這樣的格式,所以不清楚該怎麼做迴圈設定擷取資訊
請您再指點一下,如果可以的話,希望兩種都能學,附上兩個EXCEL VBA以及連結的資訊
test.zip (37.88 KB)

"URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCC/ZCC.DJHTM?A=2330"    >>>>>>>>>這是先前利用國泰證券做的連結網址,只要將最後改成迴圈變數就可以了

"URL;http://www.twse.com.tw/ch/trading/exchange/FMNPTK/genpage/Report201404/2330_F3_1_11.php?STK_NO=2330&myear=2014&mmon=04"
這是證交所上市個股2330的年成交資訊連結,不知道有沒有辦法用迴圈變數做連結

"URL;http://www.twse.com.tw/ch/trading/exchange/FMNPTK/FMNPTKMAIN.php"      >>>>>>>>>>>>>>這是證交所上市個股年成交資訊首頁


這張圖就是想要抓的表格,上面有提到希望能先匯到EXCEL再存到文字檔就是希望年的順序可以反過來,也就是越新的年度排越上面,另外資料在匯入EXCEL後會將
(元,股)也變成一列,這一列也希望能刪除

TOP

本帖最後由 GBKEE 於 2014-4-30 10:30 編輯

回復 42# smart3135
WEB 查詢請 版上有許多討論 可搜尋 http://www.twse.com.tw/ch/trading/exchange/FMNPTK/FMNPTKMAIN.php  這字串
上市與 上櫃 網頁的建置不同
  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/FMNPTK/FMNPTKMAIN.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.twse.com.tw/ch/trading/exchange/FMNPTK/FMNPTKMAIN.php"
  16.     t = Time
  17.     Application.DisplayStatusBar = True
  18.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  19.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  20.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  21.     '你已將上市的股票代號,在Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  22.     xPath = "D:\財報資料"
  23.     IE_Application    '
  24.     Application.StatusBar = " "
  25.     Sheets(1).Activate
  26.     For Each E In Rng
  27.         With IE
  28.             Do While .Busy Or .ReadyState <> 4:    Loop
  29.             Set A = .Document.getelementbyid("STK_NO")
  30.             A.Value = E
  31.              .Document.getelementSbyNAME("login_btn")(0).Click  '按下查詢
  32.             Do While .Busy Or .ReadyState <> 4:    Loop
  33.             Set A = .Document.getelementsbytagname("TABLE")
  34.             xFile = xPath & "\" & E & "\HPM.txt"
  35.             MkDir_Sub xFile
  36.              With Sheets(1)
  37.                 .Cells.Clear
  38.                 For i = 1 To A(7).Rows.Length - 1
  39.                     For C = 0 To A(7).Rows(i).Cells.Length - 1
  40.                     .Cells(i, C + 1) = A(7).Rows(i).Cells(C).innertext
  41.                     Next
  42.                 Next
  43.                 .UsedRange.Sort Key1:=.Range("A2"), Order1:=xlDescending, Header:=True  '排序:xlDescending ( 由大至小 )
  44.                 Maketxt xFile, .UsedRange
  45.             End With
  46.             ii = ii + 1
  47.         End With
  48.         Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " 共匯入上市年成交 " & ii & " 文字檔"
  49.     Next
  50.     IE.Quit
  51.     Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " 共匯入上市年成交 " & ii & " 文字檔,  讀取完畢 !! "
  52.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - t, ["MM分SS秒"])
  53.     ThisWorkbook.Save
  54. End Sub
  55. Sub Maketxt(xF As String, Q As Range)   '***   Q As Range  ****
  56.     Dim fs As Object, E As Range, C As Variant
  57.     Set fs = CreateObject("Scripting.FileSystemObject")
  58.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  59.     For Each E In Q.Rows   '修改這  Q.ResultRange.Rows
  60.     C = Application.Transpose(Application.Transpose(E.Value))
  61.         C = Join(C, vbTab)
  62.         fs.WriteLine C
  63.     Next
  64.     fs.Close
  65. End Sub
  66. Sub MkDir_Sub(S As String)
  67.     Dim AR, i As Integer, xPath As String
  68.     If Dir(S) = "" Then
  69.         AR = Split(S, "\")
  70.         xPath = AR(0)
  71.         For i = 1 To UBound(AR) - 1
  72.             xPath = xPath & "\" & AR(i)
  73.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  74.         Next
  75.     End If
  76. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 43# GBKEE
不好意思,GBKEE版主,請問是搜尋 http://www.twse.com.tw/ch/trading/exchange/FMNPTK/FMNPTKMAIN.php這字串嗎?
因為我在版上搜索這字串是查無結果的,所以想跟您確認一下,我也想好好學習網頁建置不同上的差別在哪?謝謝您!

TOP

回復 43# GBKEE
哈囉!GBKEE版主,又來向您討教囉!關於在43#的程式碼在sheet(3)的A欄套用上市個股代號後是可行的,但若我想將網址改成月成交資訊
會多了一個"年"查詢欄,又該如何設定"年"的查詢欄位可以多查幾年呢?例如首頁是直接跳出103年,再輸入股票代碼即可查詢,我想將102年、101年的資料
也一起匯入,應該怎麼做變化呢?我想關鍵是不是在以下的程式碼,因為對內容的語法不太了解,所以想再來請教您一下!
我若直接將月成交資訊的網址帶入,只能匯入103年的資料,不知道是不是各年度的月成交資訊能像股票代碼一樣在sheet(3)的某欄位輸入當成迴圈
就能匯入需要的資料,例如我想要103、102、101、100年的月成交資訊,就在sheet(3)的B欄從上往下key入,再做一些程式碼的變更就能讓程式去依照
sheet(3)的B欄輸入的年份匯入月成交資訊,不知這樣的想法對不對,再請您指點一番!謝謝!
   Do While .Busy Or .ReadyState <> 4:    Loop
            Set A = .Document.getelementbyid("STK_NO")
            A.Value = E
             .Document.getelementSbyNAME("login_btn")(0).Click  '按下查詢
            Do While .Busy Or .ReadyState <> 4:    Loop
            Set A = .Document.getelementsbytagname("TABLE")


上櫃月成交資訊





上市月成交資訊

TOP

回復 38# GBKEE
GBKEE版主您好,又來向您討教了,因為先前不懂為什麼查詢上市與上櫃年成交資訊的VBA程式碼中分別有"STK_NO"與"input stock code",之後才想到是要去找網頁原紿碼
雖然大部分都看不懂,不過確實是分別在上市與上櫃年成交資訊的網頁原始碼中找到上述兩個字串,我想這應該是必定要寫在VBA中的關鍵字串,後來去查了一下上櫃的月成交資訊
有看到多出日期欄的字串,測試了將字串寫入VBA,但卻會出錯,嘗試多次變更仍無法順利執行,不知該如何將這些字串正確地寫入VBA,當然上市的月成交資訊
測試結果也和上櫃的一樣,都是無功而返,所以只能再度叨擾您了,懇請您再次賜教,感謝!
test.zip (17.84 KB)

上櫃年成交資訊


上櫃月成交資訊

TOP

回復 46# smart3135
  1. For Each E In Rng
  2.         For Each X In Rng1
  3.             With IE
  4.          'http://forum.twbts.com/viewthread.php?tid=8111  chrom 中可查看元素的結構
  5.          '<select name="yy" class="input-select ui-corner-all" id="y_date1" onchange="query()">
  6.          '<option value="1996">85</option><option value="1997">86</option><option value="1998">87</option>         
  7.                 Set B = .document.getelementsbytagname("select")("YY")
  8.                 B.Value = X
  9.                 Set A = .document.getelementbyid("input_stock_code")
  10.                 A.Value = E
  11.    
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 47# GBKEE
感謝版主耐心的回答,看了文章之後,大概了解了相對引數的關鍵字,也有試著將相對引數"select"和"yy"代入,結果是可行的
不過有點問題:
1.在迴圈執行程式時是會依照我在sheet(3) B欄輸入的年份匯入文字檔,不過下一年份的資料又會覆蓋原來的文字檔內容
    例如我在sheet(3) B欄輸入的年份是2014、2013、2012,結果2014的寫完後再寫下一筆的2013就會把原來寫入的2014覆蓋掉
    不知道能不能將三年的資料都寫入文字檔?
2.年份迴圈是否只能利用像個股代號一樣在sheet(3) 某欄輸入想要擷取的年份資料,能不能直接寫入VBA中呢?
3.寫入的文字檔是從開始有數字資料時寫起,不知道能不能由最上方個股代號那一列開始寫入,也就是文字檔中會看得到個股代號
4.因為這個VBA程式是直接將資料寫入文字檔,無法看到資料匯入EXCEL的動作,不知道能不能做日期排序
    例如寫入的第一年份資料由上到下是103年1月份到103年5月份,不知道能不能將5月份寫到最上方
我想問題會這麼多,應該是我VBA基礎還沒打好就急於學習更進階的東西,看來我可能得多看些書、文章、影片充實自己的VBA基礎,很感謝版主連日來不厭其煩的回答!

  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/st44.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, X As Range, xPath As String, xFile As String, A, B 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, Rng1 As Range, r, C, S
  14.     Set fs = CreateObject("Scripting.FileSystemObject")
  15.     IE_URL = "http://www.gretai.org.tw/ch/stock/statistics/monthly/st44.php"
  16.     t = Time
  17.     Application.DisplayStatusBar = True
  18.     '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  19.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  20.     Set Rng1 = ThisWorkbook.Sheets(3).Range("b:b")
  21.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  22.     If Application.Count(Rng1) = 0 Then MsgBox "沒有股票代號": Exit Sub
  23.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  24.     Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants)
  25.     xPath = "D:\財報資料"
  26.     IE_Application    '
  27.     Application.StatusBar = " "
  28.     For Each E In Rng
  29.         For Each X In Rng1
  30.             With IE
  31.                 Set B = .document.getelementsbytagname("select")("yy")
  32.                 B.Value = X
  33.                 Set A = .document.getelementbyid("input_stock_code")
  34.                 A.Value = E
  35.                 A.ParentNode.submit
  36.                 Do While .Busy Or .ReadyState <> 4:    Loop
  37.                 Set A = .document.getelementsbytagname("TABLE")
  38.                 xFile = xPath & "\" & E & "\HPM.txt"
  39.                 MkDir_Sub xFile
  40.                 With fs.CreateTextFile(xFile, True)
  41.                     For i = 1 To A(2).Rows.Length - 1
  42.                         S = ""
  43.                         For C = 0 To A(2).Rows(i).Cells.Length - 1
  44.                             S = S & A(2).Rows(i).Cells(C).innertext & vbTab
  45.                         Next C
  46.                         .WriteLine S
  47.                     Next i
  48.                     .Close
  49.                 End With
  50.             ii = ii + 1
  51.             End With
  52.         Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii & " 文字檔"
  53.         Next X
  54.     Next E
  55.     IE.Quit
  56.     Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii & " 文字檔,  讀取完畢 !! "
  57.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - t, ["MM分SS秒"])
  58.     ThisWorkbook.Save
  59. End Sub
  60. Sub MkDir_Sub(S As String)
  61.     Dim AR, i As Integer, xPath As String
  62.     If Dir(S) = "" Then
  63.         AR = Split(S, "\")
  64.         xPath = AR(0)
  65.         For i = 1 To UBound(AR) - 1
  66.             xPath = xPath & "\" & AR(i)
  67.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  68.         Next
  69.     End If
  70. End Sub
  71. Sub Maketxt(xF As String, Q As QueryTable)   '將匯入資料存入指定的txt
  72.     Dim fs As Object, E As Range, C As Variant
  73.     Set fs = CreateObject("Scripting.FileSystemObject")
  74.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  75.     For Each E In Q.ResultRange.Rows
  76.         C = Application.Transpose(Application.Transpose(E.Value))
  77.         C = Join(C, vbTab)
  78.         fs.WriteLine C
  79.     Next
  80.     fs.Close
  81. End Sub
複製代碼
test2.zip (17.73 KB)

TOP

回復 48# smart3135
  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/st44.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")("yy").Value = X
  31.                  With .document.getelementbyid("input_stock_code")
  32.                     .Value = E
  33.                     .ParentNode.submit
  34.                 End With
  35.                 Do While .Busy Or .readyState <> 4:    Loop
  36.                 If Application.Count(Sheets(1).UsedRange) = 0 Then '此工作表清空時:下載第一年度時
  37.                     ar = Array(0, 2)
  38.                 Else
  39.                     ar = Array(2)
  40.                 End If
  41.                 For Each Ea In ar
  42.                     Ep .document.getElementsByTagName("TABLE")(Ea).outerHTML
  43.                 Next
  44.             ii = ii + 1
  45.             End With
  46.         Next X
  47.         xFile = xPath & "\" & E & "\HPM.txt"
  48.         MkDir_Sub xFile
  49.         Maketxt xFile, Sheets(1).UsedRange
  50.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii & " 文字檔"
  51.     Next E
  52.     IE.Quit
  53.     Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃月成交 " & ii & " 文字檔,  讀取完畢 !! "
  54.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
  55.     ThisWorkbook.Save
  56. End Sub
  57. Sub Ep(S As String)
  58.     Dim D As New DataObject, E As Shape, FormDLL As String
  59.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  60.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
  61.     On Error GoTo ER
  62.     With D
  63.         .SetText S
  64.         .PutInClipboard
  65.         With Sheets(1)
  66.             .Range("a" & .Rows.Count).End(xlUp).Offset(1).Select
  67.             .PasteSpecial Format:="Unicode 文字"
  68.         End With
  69.     End With
  70.     Exit Sub
  71. ER:
  72.     FormDLL = "FM20.DLL"
  73.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\" & FormDLL
  74.     Resume
  75. End Sub
  76. Sub MkDir_Sub(S As String)
  77.     Dim ar, i As Integer, xPath As String
  78.     If Dir(S) = "" Then
  79.         ar = Split(S, "\")
  80.         xPath = ar(0)
  81.         For i = 1 To UBound(ar) - 1
  82.             xPath = xPath & "\" & ar(i)
  83.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  84.         Next
  85.     End If
  86. End Sub
  87. Sub Maketxt(xF As String, Q As Range)    '將匯入資料存入指定的txt
  88.     Dim fs As Object, E As Range, C As Variant
  89.     Q.Range("C1") = ""
  90.     Q.Range("a4", Q.Range("a4").End(xlDown)).Replace "年", ""
  91.     Q.Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  92.     Set fs = CreateObject("Scripting.FileSystemObject")
  93.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  94.     For Each E In Q.Rows
  95.         C = Application.Transpose(Application.Transpose(E.Value))
  96.         C = Join(C, vbTab)
  97.         fs.WriteLine C
  98.     Next
  99.     fs.Close
  100. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 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

        靜思自在 : 君子立恆志,小人恆立志。
返回列表 上一主題