返回列表 上一主題 發帖

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

本帖最後由 GBKEE 於 2014-4-28 07:29 編輯

回復 35# GBKEE
感謝GBKEE版主這幾天來不斷的教導,真的讓我在EXCEL VBA上學習到很多,其他仍有許多需要擷取的資料,如資產負債表 現金流量表等,這些我都能用這幾天學到的
稍加修改就能完成,唯獨有兩種資料比較特別,我無法獨立完成,所以又要再來請教GBKEE版主了
1.
上櫃月成交資訊

 上面連結為上櫃個股月成交資訊,即使輸入個股代號,網址仍不變,據了解是因為網頁為POST,並非GET,所以沒辦法擷取報表
 不知道有沒有辦法也用VBA做迴圈來擷取資料?(在EXCEL匯入WEB資料時似乎沒法等到下面的成交資訊表格來匯入資訊)
2.請件兩個EXCEL附件,這是在網路上面找到的,主要是要抓一年內的集保資料,但都只能用自行輸入的方式來一個一個擷取資料
 不知道有沒有辦法也用類似這兩天您所提供的程式碼來抓取集保資料,結果就如附件中的TXT檔,再麻煩您指點一下囉!感謝!
   
集保資料


[attach]18178[/attach]

TOP

本帖最後由 GBKEE 於 2014-4-27 15:24 編輯

回復 34# smart3135
  1. Option Explicit
  2. Private Sub Test()
  3.     Dim fs As Object, E As Range, C As Variant
  4.     Set fs = CreateObject("Scripting.FileSystemObject")
  5.     Set fs = fs.CreateTextFile("D:\財報資料\1101\Test.TXT", True)
  6.     '創見一個檔案,如檔案存在可覆蓋掉
  7.     '文字檔的寫入是
  8.     For Each E In Sheets(1).UsedRange.Columns
  9.         C = E.Value  '-> 陣列(1 To 列數,1 To 欗數)的二維陣列
  10.         C = Application.Transpose(E.Value) '因為第二維只有一欄,轉置一次可變為一維陣列
  11.        '如E In UsedRange.rows
  12.        'C = E.Value  '-> 陣列(1 To 列數,1 To 欗數)的二維陣列
  13.        'C = Application.Transpose(Application.Transpose(E.Value)) '第二維不只一欄,所以轉置二次才可變為一維陣列
  14.         C = Join(C, vbTab)
  15.         'Join 函數 傳回一個字串,該字串是透過連結某個陣列中的多個子字串而建立的。
  16.         'Join 函數 使用的陣列必須是一維陣列
  17.         fs.WriteLine C
  18.     Next
  19.     fs.Close
  20. End Sub
複製代碼



詳看註解可明瞭.

  1. Option Explicit
  2. Sub 抓季月營收資料()
  3.     Dim E As Integer, URL As String, xPath As String, xFile As String
  4.     Dim i As Integer, ii As Integer, Rng As Range, S1 As String, S2 As String, t As Date
  5.     Dim AR()
  6.     t = Time
  7.     URL = "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCH/ZCH.DJHTM?A="
  8.     xPath = "D:\財報資料"
  9.     Application.DisplayStatusBar = True
  10.     With ThisWorkbook
  11.         '兩者取一即可 (選擇儲存格較方便)
  12.         '***'不需要的股票如4203天仁(後抓取)是錯誤的股票號碼 置入-> Ar陣列
  13.    ' AR = Array(1202, 1420, 1433, 1502, 1518, 1610, 1716, 2346, 2372, 2391, 2513, 2526, 2541, 2802, 2803, 2804, 2806, _
  14.             2813, 2814, 2815, 2817, 2818, 2819, 2821, 2826, 2830, 2839, 2840, 2843, 2844, 2848, 2907, 2909, 4101, 4112, _
  15.             4175, 4201, 4203, 4204, 4301, 4302, 4405, 4407, 4409, 4410, 4411, 4412, 4504, 4505, 4507, 4508, 4509, 4512, _
  16.             4514, 4516, 4517, 4519, 4520, 4521, 4524, 4525, 4531, 4603, 4604, 4605, 4606, 4607, 4608, 4701, 4704, 4705, _
  17.             4708, 4709, 4710, 4713, 4715, 4718, 4901, 4902, 5001, 5003, 5004, 5005, 5012, 5101, 5311, 5319, 5320, 5322, _
  18.             5323, 5327, 5330, 5331, 5334, 5335, 5337, 5341, 5342, 5354, 5357, 5358, 5359, 5360, 5361, 5362, 5363, 5366, _
  19.             5368, 5369, 5374, 5377, 5379, 5380, 5382, 5389, 5391, 5393, 5394, 5396, 5397, 5399, 5404, 5405, 5408, 5409, _
  20.             5411, 5412, 5415, 5416, 5417, 5418, 5419, 5420, 5421, 5422, 5423, 5424, 5427, 5428, 5430, 5431, 5433, 5435, _
  21.             5440, 5444, 5445, 5446, 5447, 5449, 5453, 5456, 5458, 5459, 5461, 5462, 5463, 5470, 5472, 5476, 5477, 5482, _
  22.             5485, 5486, 5495, 5496, 5499, 5509, 5517, 5527, 5606, 5705, 5804, 5805, 5806, 5807, 5809, 5812, 5814, 5815, _
  23.             5854, 6003, 6006, 6019, 6102, 6106, 6401, 6501, 8001, 8003, 8903, 8904, 8912, 8914, 8915, 8918, 8920, 8922, 8939, 9105, 9909)
  24.             
  25.         '***A欄中不需要的股票 如4203天仁(後抓取)號碼的儲存格  置入-> Ar陣列
  26.         Set Rng = .Sheets(2).Range("A:A").SpecialCells(xlCellTypeConstants)
  27.         If Rng Is Nothing Then
  28.             AR = Array()
  29.         ElseIf Rng.Count = 1 Then
  30.             AR = Array(Rng.Value)
  31.         Else
  32.             AR = Application.Transpose(Rng.Value)
  33.             
  34.             '先前出錯是這裡寫錯 AR = Application.Transpose(Application.Transpose(Rng.Value))
  35.             'Rng.Value=>單欄二維陣列
  36.         End If
  37.         '***************************************************
  38.         .Sheets(2).UsedRange.Offset(, 1).Clear
  39.         Application.ScreenUpdating = False
  40.         Application.StatusBar = " "
  41.         With .Sheets(1)      '活頁簿的第 1 張工作表
  42.             If .QueryTables.Count = 0 Then
  43.                 With .QueryTables.Add(Connection:=URL, Destination:=.Range("$A$1"))
  44.                     .Refresh BackgroundQuery:=False
  45.                 End With
  46.             End If
  47.             .Rows(1).Delete
  48.             .Columns(1).Delete
  49.             For E = 1101 To 5000
  50.                 If UBound(Filter(AR, E)) > -1 Then GoTo xLnext
  51.                 'Filter(AR, E)函數 等同使用Find方法 如 Set Rng = .Find(S2, lookat:=xlPart) '搜尋:股票名稱
  52.                 With .QueryTables(1)
  53.                     .Connection = URL & E
  54.                     .PreserveFormatting = True
  55.                     .BackgroundQuery = True
  56.                     .RefreshStyle = xlInsertDeleteCells
  57.                     .SaveData = True
  58.                     .AdjustColumnWidth = True
  59.                     .RefreshPeriod = 0
  60.                     .WebSelectionType = xlSpecifiedTables
  61.                     .WebFormatting = xlWebFormattingNone
  62.                     .WebTables = "3"
  63.                     .WebPreFormattedTextToColumns = True
  64.                     .WebConsecutiveDelimitersAsOne = True
  65.                     .Refresh BackgroundQuery:=False
  66.                     If InStr(.ResultRange(2, 1), "查無") Then      '查無這檔股票
  67.                         Mark_Code E                                '查無的股票 放置A攔
  68.                         GoTo xLnext
  69.                     End If
  70.                     S1 = .ResultRange(1)                '匯入資料的第1個儲存格
  71.                     S2 = Mid(S1, 1, InStr(S1, "(") - 1) '股票名稱
  72.                 End With
  73.                
  74.                 With ThisWorkbook.Sheets(2).Range("B:B")
  75.                     Set Rng = .Find(S2, lookat:=xlPart) '搜尋:股票名稱
  76.                     If Rng Is Nothing Then
  77.                         i = i + 1
  78.                         .Range("A" & i) = S1  '股票名稱代碼
  79.                     Else
  80.                        Mark_Code E '重複的股票 放置A攔
  81.                         S2 = Mid(Trim(Rng), InStr(Trim(Rng), "(") + 1)
  82.                         S2 = Mid(S2, 1, Len(S2) - 1)    '舊的股票[代碼]
  83.                         xFile = xPath & "\" & S2 & "\*.*" '殺掉所有檔案
  84.                         If Dir(xFile) <> "" Then
  85.                             ii = ii - 1
  86.                             Kill xFile
  87.                             xFile = xPath & "\" & S2
  88.                             If Dir(xFile, vbDirectory) <> "" Then RmDir xFile '資料夾也刪除了
  89.                         End If
  90.                     End If
  91.                 End With
  92.                 ii = ii + 1
  93.                 xFile = xPath & "\" & E & "\REVENUE.txt"
  94.                 MkDir_Sub xFile
  95.                 Maketxt xFile, .QueryTables(1)
  96. xLnext:
  97.                 S1 = " " & Sheets(1).QueryTables(1).ResultRange(1)
  98.                 If Val(S1) < 0 Then S1 = " 查無"
  99.                 Application.StatusBar = Application.text(Time - t, ["MM分SS秒"]) & " 共匯入 " & ii & " 文字檔,  讀取 " & S1 & " 中..."
  100.             Next
  101.         End With
  102.     End With
  103.     Application.ScreenUpdating = True
  104.     Application.StatusBar = Application.text(Time - t, ["MM分SS秒"]) & " 共匯入 " & ii & " 文字檔,  讀取完畢 !! "
  105.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.text(Time - t, ["MM分SS秒"])
  106.     ThisWorkbook.Save
  107. End Sub
複製代碼
  1. Private Sub Mark_Code(S As Integer)
  2.     With ThisWorkbook.Sheets(2).Range("A:A") '重複的股票 放置A攔
  3.         .Cells(Application.Count(.Cells) + 1, "A") = S
  4.         '股票代號放置 A攔最後一個儲存格
  5.         '執行一次後,下次再執行 [抓季月營收資料]程式時,For 迴圈中可踢除這股票代號
  6.     End With
  7. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 27# GBKEE
GBKEE版主早安,有些問題想請教您:
1.今早下班回來就馬上試了您昨天在27#提供的程式碼,我有再加上要跳過的代號在AR中,也照提示將代號都輸入到sheet(2)的A欄
直接跑一次,奇怪的事發生了,我設定For E = 1101 to 5000,每次跑到一半時就會出錯,出錯的位置是在If UBound(Filter(AR, E)) > -1 And UBound(AR) > -1 Then,不清楚哪裡出了問題
附上程式碼和檔案,請您幫忙看看 
2.另外我有發現您27#的程式碼在出錯前的執行時速度非常快,和您先前提供的程式碼讓我在23#完成的程式有很大的落差,這當中究竟有什麼差異呢?為什麼速度會差這麼多呢?
一樣我兩個VBA程式都有附上,也麻煩您幫忙看看為什麼會有如此大的差異
3.我用您先前提供的跑下來,第一段1101 to 5000,費時12分48秒,抓了992筆資料
第二段5001 to 9962,費時13分50秒,抓了515筆資料
我在跑第二段時有先使用清除系統的.bat檔,並將EXCEL關閉再開啟重新執行VBA,結果看來並沒有瘦身的效果,不知道為什麼會這樣?
4.另外在將資料匯入txt的程式碼中,如下
   For Each E In Q.ResultRange.Rows
        C = Application.Transpose(Application.Transpose(E.Value))
        C = Join(C, vbTab)
        fs.WriteLine C
    Next
其中Q.ResultRange.Rows的Rows是不是代表列,也就是將資料一列一列存入txt,直到沒有資料為止
之後我有想到,因為用一列一列的方式來匯入資料要跑很多次迴圈,如果是用一欄一欄的方式匯入就會少跑很多次迴圈
我有試著將Rows改為Columns,但執行到下二行的C = Join(C, vbTab)就會出錯,不知道有沒有辦法用欄的方式匯入呢?

問題有點多,再麻煩您幫忙一下囉!感謝!
  1. Option Explicit
  2. Sub 抓季月營收資料()
  3.     Dim E As Integer, URL As String, xPath As String, xFile As String
  4.     Dim i As Integer, ii As Integer, Rng As Range, S1 As String, S2 As String, t As Date
  5.     Dim AR()
  6.     t = Time
  7.     AR = Array(1202, 1420, 1433, 1502, 1518, 1610, 1716, 2346, 2372, 2391, 2513, 2526, 2541, 2802, 2803, 2804, 2806, _
  8.             2813, 2814, 2815, 2817, 2818, 2819, 2821, 2826, 2830, 2839, 2840, 2843, 2844, 2848, 2907, 2909, 4101, 4112, _
  9.             4175, 4201, 4203, 4204, 4301, 4302, 4405, 4407, 4409, 4410, 4411, 4412, 4504, 4505, 4507, 4508, 4509, 4512, _
  10.             4514, 4516, 4517, 4519, 4520, 4521, 4524, 4525, 4531, 4603, 4604, 4605, 4606, 4607, 4608, 4701, 4704, 4705, _
  11.             4708, 4709, 4710, 4713, 4715, 4718, 4901, 4902, 5001, 5003, 5004, 5005, 5012, 5101, 5311, 5319, 5320, 5322, _
  12.             5323, 5327, 5330, 5331, 5334, 5335, 5337, 5341, 5342, 5354, 5357, 5358, 5359, 5360, 5361, 5362, 5363, 5366, _
  13.             5368, 5369, 5374, 5377, 5379, 5380, 5382, 5389, 5391, 5393, 5394, 5396, 5397, 5399, 5404, 5405, 5408, 5409, _
  14.             5411, 5412, 5415, 5416, 5417, 5418, 5419, 5420, 5421, 5422, 5423, 5424, 5427, 5428, 5430, 5431, 5433, 5435, _
  15.             5440, 5444, 5445, 5446, 5447, 5449, 5453, 5456, 5458, 5459, 5461, 5462, 5463, 5470, 5472, 5476, 5477, 5482, _
  16.             5485, 5486, 5495, 5496, 5499, 5509, 5517, 5527, 5606, 5705, 5804, 5805, 5806, 5807, 5809, 5812, 5814, 5815, _
  17.             5854, 6003, 6006, 6019, 6102, 6106, 6401, 6501, 8001, 8003, 8903, 8904, 8912, 8914, 8915, 8918, 8920, 8922, 8939, 9105, 9909)
  18.             '輸入 4203天仁(後抓取)是錯誤的股票號碼
  19.     URL = "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCH/ZCH.DJHTM?A="
  20.     xPath = "G:\財報資料"
  21.     With ThisWorkbook
  22.         .Sheets(2).UsedRange.Offset(, 1).Clear
  23.         '4203天仁(後抓取)是錯誤的股票號碼 這些 股票名稱(代號) 連續一起輸入在Sheets(2)的A欄
  24.         Set Rng = .Sheets(2).Range("A:A").SpecialCells(xlCellTypeConstants)
  25.         If Rng Is Nothing Then
  26.             AR = Array()
  27.         ElseIf Rng.Count = 1 Then
  28.             AR = Array(Rng.Value)
  29.         Else
  30.             AR = Application.Transpose(Application.Transpose(Rng))
  31.         End If        '***************************************************
  32.         Application.ScreenUpdating = False
  33.         Application.StatusBar = " "
  34.         With .Sheets(1)      '活頁簿的第 1 張工作表
  35.             If .QueryTables.Count = 0 Then
  36.                 With .QueryTables.Add(Connection:=URL, Destination:=.Range("$A$1"))
  37.                     .Refresh BackgroundQuery:=False
  38.                 End With
  39.             End If
  40.                 .Rows(1).Delete
  41.                 .Columns(1).Delete
  42.             For E = 1101 To 5000
  43.                 With .QueryTables(1)
  44.                     .Connection = URL & E
  45.                     .PreserveFormatting = True
  46.                     .BackgroundQuery = True
  47.                     .RefreshStyle = xlInsertDeleteCells
  48.                     .SaveData = True
  49.                     .AdjustColumnWidth = True
  50.                     .RefreshPeriod = 0
  51.                     .WebSelectionType = xlSpecifiedTables
  52.                     .WebFormatting = xlWebFormattingNone
  53.                     .WebTables = "3"
  54.                     .WebPreFormattedTextToColumns = True
  55.                     .WebConsecutiveDelimitersAsOne = True
  56.                     .Refresh BackgroundQuery:=False
  57.                     If .ResultRange(1) < 0 Or InStr(.ResultRange(2, 1), "查無") Then GoTo xLnext
  58.                     '匯入資料的 A1 < 0  OR  匯入資料的 A2 "查無"
  59.                     S1 = .ResultRange(1)
  60.                     S2 = Mid(S1, 1, InStr(S1, "(") - 1) '股票名稱
  61.                 End With
  62.                 With ThisWorkbook.Sheets(2).Range("B:B")
  63.                     Set Rng = .Find(S2, lookat:=xlPart) '搜尋:股票名稱
  64.                     If Rng Is Nothing Then
  65.                         i = i + 1
  66.                         .Range("A" & i) = S1  '股票名稱代碼
  67.                     Else
  68.                         Rng.Cells(1, 2) = S1   '重複的股票
  69.                         If UBound(Filter(AR, E)) > -1 And UBound(AR) > -1 Then
  70.                         'Filter(AR, E) > -1   '比對到如4203天仁(後抓取)是錯誤
  71.                             Rng.Cells(1, 2) = Rng.Cells(1, 2) & "***" '後抓取是錯誤
  72.                             GoTo xLnext:
  73.                         End If
  74.                         S2 = Mid(Trim(Rng), InStr(Trim(Rng), "(") + 1)
  75.                         S2 = Mid(S2, 1, Len(S2) - 1)    '舊的股票[代碼]
  76.                         xFile = xPath & "\" & S2 & "\*.*" '殺掉所有檔案
  77.                         If Dir(xFile) <> "" Then
  78.                             ii = ii - 1
  79.                             Kill xFile
  80.                             xFile = xPath & "\" & S2
  81.                             If Dir(xFile, vbDirectory) <> "" Then RmDir xFile '資料夾也刪除了
  82.                         End If
  83.                     End If
  84.                 End With
  85.                 ii = ii + 1
  86.                 xFile = xPath & "\" & E & "\REVENUE.txt"
  87.                 MkDir_Sub xFile
  88.                 Maketxt xFile, .QueryTables(1)
  89. xLnext:
  90.                 S1 = " " & Sheets(1).QueryTables(1).ResultRange(1)
  91.                 If Val(S1) < 0 Then S1 = " 查無"
  92.                 Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & "  " & E & S1
  93.             Next
  94.         End With
  95.     End With
  96.     Application.ScreenUpdating = True
  97.     Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " Ok "
  98.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - t, ["MM分SS秒"])
  99. End Sub
  100. Private 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
  111. Private Sub Maketxt(xF As String, Q As QueryTable)   '將匯入資料存入指定的txt
  112.     Dim fs As Object, E As Range, C As Variant
  113.     Set fs = CreateObject("Scripting.FileSystemObject")
  114.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  115.     For Each E In Q.ResultRange.Rows
  116.         C = Application.Transpose(Application.Transpose(E.Value))
  117.         C = Join(C, vbTab)
  118.         fs.WriteLine C
  119.     Next
  120.     fs.Close
  121. End Sub
複製代碼
兩個月營收VBA.zip (38.38 KB)

TOP

回復 32# GBKEE
哇 1101到5000跑完只要七分鐘 好威!
我之前也有試過接下引號跳下一行,但一直失敗,原來要跳下一行前接的下引號後面要有空格,又學到一招了,再次感謝您的指導!

TOP

本帖最後由 GBKEE 於 2014-4-26 20:22 編輯

回復 31# smart3135
這個BAT檔我一直都有在用,是否每次執行完VBA就要清一次呢?,你要跑這程式當然最好是的,檔案開開關關會製造一些暫存檔,就需要清一下
WIN7 64位元+office 2007,記憶體是4G,是不需擴充了,另就就開機時系統要載太入的常駐應用程式.佔據太多的記憶體.這也是主因之ㄧ.
XP,32位元, 1GB, 2003版, 27#程式 For 1101 To 5000的迴圈 ,減肥後7分鐘內搞定.
E的區間設定越小,完成的時間就越短,這部分就不太理解為什麼會這樣了,這是想當然的
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Ar
  4.     Ar = Array(56, 78, _
  5.          9, 10, 12 _
  6.        , 888, 999, _
  7.        56789)
  8. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 30# GBKEE
先感謝GBKEE版主一一耐心的回答,因為今晚還要上班,所以您在27#更新的程式碼,可能要等到明天才能try了!
至於電腦減肥部分
1將下面文字複製到記事本  存檔為附檔名 ".BAT",傳送到桌面上 ,不定時的清理垃圾檔案-這個BAT檔我一直都有在用,是否每次執行完VBA就要清一次呢?
2不定時的清空資源回收筒 -最近丟到資源回收筒的資料較多,有空會試一下清空會不會好一點
3 不定時清空IE的瀏覽記錄-平常都是用chorme流灠器為主,不過最近因為要EXCEL匯入WEB資料所以有比較常用,會清空再來試試
4 定時的清理磁碟-兩天前才剛將磁碟重組
5擴充記憶體-我的系統是WIN7 64位元+office 2007,記憶體是4G,不知道這樣有需要擴充嗎?

我有發現會跑那麼久是因為我給的區間越大,程式跑到越後面就越慢,例如我程式碼設定For E = 1101 to 9962,只執行一次,跑起來可能需要40分鐘
但我將程式碼設定For E = 1101 to 3000,For E = 3001 to 5000,For E = 5001 to 7000,For E = 7001 to 9962,共執行四次,合計起來的時間就不需要那麼久
E的區間設定越小,完成的時間就越短,這部分就不太理解為什麼會這樣了!

另外雖然您在27#的程式碼已更新,不過還是希望能了解一下我在28#向您提問的AR=Array(1202,1433,1502,1610.....................)這樣要把200個代碼全部輸入
會跳到第二行,然後就會出錯,不知道有沒有辦法解決這個問題呢?

不知道這個AR的Array區間有沒有辦法輸入200個引數以上呢?

TOP

本帖最後由 GBKEE 於 2014-4-26 15:52 編輯

回復 29# smart3135
1233天仁(先抓取)是正確的,4203天仁(後抓取)是錯誤的問題
這一些錯誤股票名稱(代號) 天仁(4203) 連續一起輸入在Sheets(2)的A欄,後程式可解決.

28# 的所有問題.27#程式碼已更新了,可再看一次,減肥有試一下嗎?

試試看
  1. Sub Ex()
  2.     Dim E As Integer
  3.     For E = 500 To 5000
  4.         Select Case E
  5.             Case 500 To 1000
  6.                 GoTo xNext
  7.             Case 1500 To 2000
  8.                 GoTo xNext
  9.             Case 2500 To 3000
  10.                 GoTo xNext
  11.         End Select
  12.         MsgBox E
  13. xNext:
  14.     Next
  15. End Sub
  16. Sub Ex1()
  17.     Dim E As Integer
  18.     For E = 500 To 5000
  19.         If E >= 500 And E <= 1000 Or E >= 1500 And E <= 2000 Or E >= 2500 And E <= 3000 Then
  20.                 GoTo xNext
  21.         End If
  22.         MsgBox E
  23. xNext:
  24.     Next
  25. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 27# GBKEE
版主,不好意思,再請教一個問題,現在我要設定迴圈為for E = 1101  9962,但我已經知道某些數字區間是不需要去擷取的,如果想跳過該使用怎樣的語法呢?先謝謝您!
大概的構想如下:
  1. Dim E As Integer
  2.                          For E = 1101 To 2000
  3.                          IF E = 3800到4100 then goto xlNext '想設定某個區間,請教語法該怎麼設
  4.                          IF E = 6850到8000 then goto xlNext '想設定某個區間,請教語法該怎麼設
  5.                          IF E = 8550到9000 then goto xlNext '想設定某個區間,請教語法該怎麼設
  6.                          IF E = 9000到9800 then goto xlNext '想設定某個區間,請教語法該怎麼設,共四個區間

  7. xlNext:         
  8.                          next
  9. End sub
複製代碼

TOP

回復 27# GBKEE
GBKEE版主您好,今早下班後就開始在Try您在27#回覆的程式碼,結果的確會將一些重覆的個股txt刪除,但不知有沒有辦法將一起建立的資料夾也刪除呢?
舉例來說,1202和2913兩個都是農林,所以程式執行完會將1202的txt刪除,但1202的資料夾仍是存在的,不知沒有沒辦法連資料夾一起刪除呢?
另外還有一個問題,就是這個程式碼保留的資料都是sheet(2)的第二欄個股代號資料,若我沒解讀錯誤的話,程式應該是將個股代碼相同,先抓取的txt刪除,保留後抓取的txt
但就會遇到1233天仁(先抓取)是正確的,4203天仁(後抓取)是錯誤的問題,結果就是正確的1233天仁txt被刪除,這部分我想應該不太好解決
所以,如果可以的話,我還是傾向在您24#回覆的程式碼,用一個一個挑出的方式,這些不需要的代號我都有了,只要輸入AR=Array()中就可以了,只是我要輸入的代號
大概有200個左右,如果全部輸入,如AR=Array(1202,1433,1502,1610.....................)這樣要把200個代碼全部輸入會跳到第二行,然後就會出錯,不知道有沒有辦法
解決這個問題呢?先感謝您的指導!

TOP

本帖最後由 GBKEE 於 2014-4-26 15:42 編輯

回復 26# smart3135
大約要耗時40分鐘以上,是有點久,電腦要減肥了
建議減肥方式如下
1將下面文字複製到記事本  存檔為附檔名 ".BAT",傳送到桌面上 ,不定時的清理垃圾檔案
2不定時的清空資源回收筒
3 不定時清空IE的瀏覽記錄
4 定時的清理磁碟
5擴充記憶體

4203天仁(後抓取)是錯誤的股票號碼,這些股票名稱(代號) 連續一起輸入在Sheets(2)的A欄
  1. Option Explicit
  2. Sub 抓季月營收資料()
  3.     Dim E As Integer, URL As String, xPath As String, xFile As String
  4.     Dim i As Integer, ii As Integer, Rng As Range, S1 As String, S2 As String, t As Date
  5.     Dim AR()
  6.     t = Time
  7.     AR = Array(4203) '輸入 4203天仁(後抓取)是錯誤的股票號碼
  8.     URL = "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCH/ZCH.DJHTM?A="
  9.     xPath = "D:\財報資料"
  10.     With ThisWorkbook
  11.         .Sheets(2).UsedRange.Offset(, 1).Clear
  12.         '4203天仁(後抓取)是錯誤的股票號碼 這些 股票名稱(代號) 連續一起輸入在Sheets(2)的A欄
  13.         Set Rng = .Sheets(2).Range("A:A").SpecialCells(xlCellTypeConstants)
  14.         If Rng Is Nothing Then
  15.             AR = Array()
  16.         ElseIf Rng.Count = 1 Then
  17.             AR = Array(Rng.Value)
  18.         Else
  19.             AR = Application.Transpose(Application.Transpose(Rng))
  20.         End If        '***************************************************
  21.         Application.ScreenUpdating = False
  22.         Application.StatusBar = " "
  23.         With .Sheets(1)      '活頁簿的第 1 張工作表
  24.             If .QueryTables.Count = 0 Then
  25.                 With .QueryTables.Add(Connection:=URL, Destination:=.Range("$A$1"))
  26.                     .Refresh BackgroundQuery:=False
  27.                 End With
  28.             End If
  29.                 .Rows(1).Delete
  30.                 .Columns(1).Delete
  31.             For E = 1101 To 5000
  32.                 With .QueryTables(1)
  33.                     .Connection = URL & E
  34.                     .PreserveFormatting = True
  35.                     .BackgroundQuery = True
  36.                     .RefreshStyle = xlInsertDeleteCells
  37.                     .SaveData = True
  38.                     .AdjustColumnWidth = True
  39.                     .RefreshPeriod = 0
  40.                     .WebSelectionType = xlSpecifiedTables
  41.                     .WebFormatting = xlWebFormattingNone
  42.                     .WebTables = "3"
  43.                     .WebPreFormattedTextToColumns = True
  44.                     .WebConsecutiveDelimitersAsOne = True
  45.                     .Refresh BackgroundQuery:=False
  46.                     If .ResultRange(1) < 0 Or InStr(.ResultRange(2, 1), "查無") Then GoTo xLnext
  47.                     '匯入資料的 A1 < 0  OR  匯入資料的 A2 "查無"
  48.                     S1 = .ResultRange(1)
  49.                     S2 = Mid(S1, 1, InStr(S1, "(") - 1) '股票名稱
  50.                 End With
  51.                 With ThisWorkbook.Sheets(2).Range("B:B")
  52.                     Set Rng = .Find(S2, lookat:=xlPart) '搜尋:股票名稱
  53.                     If Rng Is Nothing Then
  54.                         i = i + 1
  55.                         .Range("A" & i) = S1  '股票名稱代碼
  56.                     Else
  57.                         Rng.Cells(1, 2) = S1   '重複的股票
  58.                         If UBound(Filter(AR, E)) > -1 And UBound(AR) > -1 Then
  59.                         'Filter(AR, E) > -1   '比對到如4203天仁(後抓取)是錯誤
  60.                             Rng.Cells(1, 2) = Rng.Cells(1, 2) & "***" '後抓取是錯誤
  61.                             GoTo xLnext:
  62.                         End If
  63.                         S2 = Mid(Trim(Rng), InStr(Trim(Rng), "(") + 1)
  64.                         S2 = Mid(S2, 1, Len(S2) - 1)    '舊的股票[代碼]
  65.                         xFile = xPath & "\" & S2 & "\*.*" '殺掉所有檔案
  66.                         If Dir(xFile) <> "" Then
  67.                             ii = ii - 1
  68.                             Kill xFile
  69.                             xFile = xPath & "\" & S2
  70.                             If Dir(xFile, vbDirectory) <> "" Then RmDir xFile '資料夾也刪除了
  71.                         End If
  72.                     End If
  73.                 End With
  74.                 ii = ii + 1
  75.                 xFile = xPath & "\" & E & "\REVENUE.txt"
  76.                 MkDir_Sub xFile
  77.                 Maketxt xFile, .QueryTables(1)
  78. xLnext:
  79.                 S1 = " " & Sheets(1).QueryTables(1).ResultRange(1)
  80.                 If Val(S1) < 0 Then S1 = " 查無"
  81.                 Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & "  " & E & S1
  82.             Next
  83.         End With
  84.     End With
  85.     Application.ScreenUpdating = True
  86.     Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " Ok "
  87.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - t, ["MM分SS秒"])
  88. End Sub
  89. Private Sub Maketxt(xF As String, Q As QueryTable)   '將匯入資料存入指定的txt
  90.     Dim fs As Object, E As Range, C As Variant
  91.     Set fs = CreateObject("Scripting.FileSystemObject")
  92.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  93.     For Each E In Q.ResultRange.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
複製代碼

EX.JPG (136.56 KB)

EX.JPG

感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 太陽光大、父母恩大、君子量大,小人氣大。
返回列表 上一主題