返回列表 上一主題 發帖

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

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

上櫃年成交資訊


上櫃月成交資訊

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

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

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

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

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

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

TOP

回復 39# smart3135
跑一個文字檔的時間大約要4-5秒,確定有清理系統了,不過還是非常慢 ?
可能是越老的版本速度越快,試試2003版看看
另外有辦法像之前將匯入資料顯示在EXCEL以便逐行執行時可以看出EXCEL如何變化,如此速度更慢了.

  1. Option Explicit
  2. Sub 集保戶股權_WEB_製圖()
  3.     Dim WB As Workbook, Rng As Range
  4.     Dim Ar(), A, i As Integer, strDate As String, stkno As String, Qur As String
  5.     With CreateObject("InternetExplorer.Application")
  6.         .Navigate "http://www.tdcc.com.tw/smWeb/QryStock.jsp"
  7.         Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
  8.         Set A = .document.All.tags("option") '資料日期的內容
  9.         ReDim Ar(A.Length - 1)
  10.         For i = 0 To A.Length - 1
  11.             Ar(i) = A(i).innerHTML
  12.         Next
  13.         .Quit
  14.     End With
  15.     Set WB = Workbooks.Add
  16.     With WB
  17.         .Sheets(1).Name = "圖表"
  18.         .Sheets(2).Name = "資料庫"
  19.     End With
  20.     stkno = InputBox("輸入股票代號", "股票代號", 2317)    '
  21.     If stkno = "" Or Len(stkno) <> 4 Or Val(stkno) = 0 Then MsgBox "股票代號"
  22.     For i = 0 To UBound(Ar)
  23.         strDate = Ar(i)  '導入月份
  24.         Qur = "http://www.tdcc.com.tw/smWeb/QryStock.jsp?SCA_DATE=" & strDate & "&SqlMethod=StockNo&StockNo=" & stkno & "&StockName=&sub=%ACd%B8%DF"          '
  25.         With WB.Sheets(3)
  26.             If .QueryTables.Count = 0 Then
  27.                 .QueryTables.Add "URL;" & Qur, .[A1]
  28.             Else
  29.                 .QueryTables(1).Connection = "URL;" & Qur
  30.             End If
  31.             With .QueryTables(1)
  32.                 .WebSelectionType = xlSpecifiedTables
  33.                 .WebFormatting = xlWebFormattingNone
  34.                 .WebTables = "6,7,8"
  35.                 .WebPreFormattedTextToColumns = True
  36.                 .WebConsecutiveDelimitersAsOne = True
  37.                 .WebSingleBlockTextImport = False
  38.                 .WebDisableDateRecognition = False
  39.                 .WebDisableRedirections = False
  40.                 .Refresh BackgroundQuery:=False
  41.                 If Application.Count(.ResultRange) = 0 Then
  42.                     MsgBox "股票代號 " & stkno & " 錯誤"
  43.                     WB.Close , False
  44.                     Exit Sub
  45.                 End If
  46.                 If i = 0 Then WB.Sheets(2).Cells(2, i + 1).Resize(15) = .ResultRange.Range("B6").Resize(15).Value
  47.                 WB.Sheets(2).Cells(1, i + 2) = Mid(.ResultRange.Range("A3"), 6)
  48.                 WB.Sheets(2).Cells(2, i + 2).Resize(15) = .ResultRange.Range("E6").Resize(15).Value
  49.             End With
  50.         End With
  51.     Next
  52.     With WB
  53.       Set Rng = .Sheets(2).UsedRange
  54.        With .Sheets(1)
  55.             With .ChartObjects.Add(.[B3].Left, .[B3].Top, .[B3].Resize(, 15).Width, .[B3].Resize(25).Height).Chart
  56.                 .ChartType = xlLineMarkers
  57.                 .SetSourceData Rng, xlRows
  58.                 .SeriesCollection(13).AxisGroup = 2
  59.                 .HasTitle = True
  60.                 .ChartTitle.Characters.Text = WB.Sheets(3).[A1]
  61.                 .PlotArea.Interior.ColorIndex = 23
  62.             End With
  63.         End With
  64.         .Activate
  65.     End With
  66. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 38# GBKEE
GBKEE版主您好,這個程式碼我有試了,可以成功,不過不知道為什麼跑起來非常慢,上櫃公司統計共663家,我將它輸入到Sheets(3)的A欄
跑一個文字檔的時間大約要4-5秒,確定有清理系統了,不過還是非常慢
另外有辦法像之前將匯入資料顯示在EXCEL以便逐行執行時可以看出EXCEL如何變化,麻煩您了,謝謝!
  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 = "G:\財報資料"
  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 & "\HPM.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
複製代碼

TOP

回復 37# 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/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
  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 & "\上櫃月成交.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
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 35# GBKEE
抱歉 檔案不知道為什麼上傳失敗 所以再上傳一次
集保資料.zip (74.96 KB)

TOP

        靜思自在 : 唯其尊重自己的人,才更勇於縮小自己。
返回列表 上一主題