返回列表 上一主題 發帖

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

回復 2# smart3135
試試看
  1. Option Explicit
  2. Sub 抓季損益表資料()
  3.     Dim Rng As Range, AR(), URL As String, E As Variant
  4.     For E = Sheets.Count To 2 Step -1
  5.         Sheets(E).Delete   '工作表刪除
  6.     Next
  7.     AR = Array(2303, 2485, 2030)
  8.     Sheets(1).Range("A1:A3") = Application.WorksheetFunction.Transpose(AR)
  9.     Set Rng = Sheets(1).Range("A1:A3")
  10.     For Each E In Rng   '迴圈 儲存格
  11.     ' For Each E In AR  '迴圈 也可以用陣列
  12.         With Sheets.Add(, Sheets(1))  '新增工作表
  13.             .Name = E
  14.             URL = "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCQ/ZCQ.DJHTM?A=" & E
  15.             With .QueryTables.Add(Connection:=URL, Destination:=.Range("$A$1"))
  16.                 .Name = "ZCQ.DJHTM?A=" & E
  17.                 .PreserveFormatting = True
  18.                 .BackgroundQuery = True
  19.                 .RefreshStyle = xlInsertDeleteCells
  20.                 .SaveData = True
  21.                 .AdjustColumnWidth = True
  22.                 .RefreshPeriod = 0
  23.                 .WebSelectionType = xlSpecifiedTables
  24.                 .WebFormatting = xlWebFormattingNone
  25.                 .WebTables = "3"
  26.                 .WebPreFormattedTextToColumns = True
  27.                 .WebConsecutiveDelimitersAsOne = True
  28.                 .Refresh BackgroundQuery:=False
  29.             End With
  30.         End With
  31.     Next
  32. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 5# smart3135
  1. Option Explicit
  2. Sub 抓季損益表資料()
  3.     Dim E As Integer
  4.     Application.DisplayAlerts = False  '停止系統的警告提示
  5.     For E = 1101 To 2330
  6.         With Sheets.Add(, Sheets(1))   '新增工作表
  7.             .Name = E
  8.             URL = "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCQ/ZCQ.DJHTM?A=" & E
  9.             With .QueryTables.Add(Connection:=URL, Destination:=.Range("$A$1"))
  10.                 .Name = "ZCQ.DJHTM?A=" & E
  11.                 .PreserveFormatting = True
  12.                 .BackgroundQuery = True
  13.                 .RefreshStyle = xlInsertDeleteCells
  14.                 .SaveData = True
  15.                 .AdjustColumnWidth = True
  16.                 .RefreshPeriod = 0
  17.                 .WebSelectionType = xlSpecifiedTables
  18.                 .WebFormatting = xlWebFormattingNone
  19.                 .WebTables = "3"
  20.                 .WebPreFormattedTextToColumns = True
  21.                 .WebConsecutiveDelimitersAsOne = True
  22.                 .Refresh BackgroundQuery:=False
  23.             End With
  24.             
  25.             If .[A1] = -E Then  '這網頁如股票代碼錯誤會傳回負號.
  26.                 Stop            'STOP 是給你回到工作表驗證用,可刪掉這程式碼
  27.                 ActiveSheet.Delete
  28.             End If
  29.         End With
  30.     Next
  31.     Application.DisplayAlerts = True   '程式結速:恢復系統的警告提示
  32. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2014-4-24 09:11 編輯

回復 7# smart3135
試試看
  1. Option Explicit
  2. Sub 抓季損益表資料()
  3.     Dim E As Integer, URL As String, xPath As String
  4.     URL = "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCQ/ZCQ.DJHTM?A="
  5.     xPath = "C:\季損益表"
  6.     '存檔路徑是C:\E\XYZ.TXT, 建議改為 C:\季損益表\1101.txt
  7.     With ThisWorkbook
  8.        ' If .Sheets.Count = 1 Then .Sheets.Add  '配合讀取txt檔到工作表時必須有2張工作表
  9.         With .Sheets(1)   '活頁簿的第 1 張工作表
  10.             If .QueryTables.Count = 0 Then
  11.                 With .QueryTables.Add(Connection:=URL, Destination:=.Range("$A$1"))
  12.                     .Refresh BackgroundQuery:=False
  13.                 End With
  14.             End If
  15.             For E = 1101 To 2330
  16.                 With .QueryTables(1)
  17.                     .Connection = URL & E
  18.                     .PreserveFormatting = True
  19.                     .BackgroundQuery = True
  20.                     .RefreshStyle = xlInsertDeleteCells
  21.                     .SaveData = True
  22.                     .AdjustColumnWidth = True
  23.                     .RefreshPeriod = 0
  24.                     .WebSelectionType = xlSpecifiedTables
  25.                     .WebFormatting = xlWebFormattingNone
  26.                     .WebTables = "3"
  27.                     .WebPreFormattedTextToColumns = True
  28.                     .WebConsecutiveDelimitersAsOne = True
  29.                     .Refresh BackgroundQuery:=False
  30.                 End With
  31.                 If .[A1] <> -E Then  '這網頁如股票代碼錯誤會傳回負號.
  32.                     If Dir(xPath, vbDirectory) = "" Then MkDir xPath '目錄不存在則新徵增此目錄
  33.                     Maketxt xPath & "\" & E & ".TXT", .QueryTables(1)
  34.                     'Redalltxt xPath & "\" & E & ".TXT"  '讀取txt檔到工作表
  35.                 End If
  36.             Next
  37.         End With
  38.     End With
  39. End Sub
  40. Sub Maketxt(xF As String, Q As QueryTable)   '將匯入資料存入指定的txt
  41.     Dim fs As Object, E As Range, C As Variant
  42.     Set fs = CreateObject("Scripting.FileSystemObject")
  43.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  44.     For Each E In Q.ResultRange.Rows
  45.         C = Application.Transpose(Application.Transpose(E.Value))
  46.         C = Join(C, vbTab)
  47.         fs.WriteLine C
  48.     Next
  49.     fs.Close
  50. End Sub
  51. Sub Redalltxt(xF As String)   '
  52.     Dim fs As Object, E, D As New DataObject
  53.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  54.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
  55.     Set fs = CreateObject("Scripting.FileSystemObject")
  56.     Set fs = fs.OpenTextFile(xF, 1)
  57.      E = fs.readall
  58.     fs.Close
  59.     With D
  60.         .SetText E
  61.         .PutInClipboard
  62.         With Sheets(2)
  63.             .UsedRange.Clear
  64.             .Activate
  65.             .Range("A1").Select
  66.             .PasteSpecial Format:="Unicode 文字"
  67.             .Cells.Font.Size = 12
  68.             .Cells.Font.Bold = False
  69.             .Cells.EntireColumn.AutoFit
  70.         End With
  71.     End With
  72. End Sub
  73. Sub Set_FormDLL()   '新增引用 Microsoft Forms 2.0 Object Library
  74.     On Error Resume Next
  75.     FormDLL = "FM20.DLL"
  76.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\" & FormDLL
  77. '2003版的目錄為 C:\windows\system32\ ,你需修改此目錄
  78. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2014-4-25 05:45 編輯

回復 9# smart3135
  1. Option Explicit
  2. Sub Ex()
  3.     MkDir_Sub "D:\Test\季損益表\1103\ISQ.txt"
  4. End Sub
  5. Sub MkDir_Sub(S As String)
  6.     Dim AR, i As Integer, xPath As String
  7.     If Dir(S) = "" Then
  8.         AR = Split(S, "\")
  9.         '如S參數所接收字串="D:\MYSUB\ABC\1101.TXT
  10.         'Split函數將S以"\"分割為陣列
  11.         'AR(0) = "D:"
  12.         'AR(1) = "MYSUB"
  13.         'AR(2) = "ABC"
  14.         'AR(3) = "1101.TXT"
  15.         'UBound(AR)傳回AR陣列的上限元素索引值,3
  16.         xPath = AR(0)
  17.         For i = 1 To UBound(AR) - 1
  18.             xPath = xPath & "\" & AR(i)
  19.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  20.             'MkDir : 創建目錄
  21.         Next
  22.     End If
  23. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2014-4-24 17:04 編輯

回復 12# smart3135
  1. Option Explicit
  2. Sub 抓季損益表資料()
  3.     Dim E As Integer, URL As String, xPath As String, xFile As String
  4.     Dim Msg As Boolean
  5.     URL = "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCQ/ZCQ.DJHTM?A="
  6.     xPath = "C:\季損益表"
  7.     With ThisWorkbook
  8.         With .Sheets(1)      '活頁簿的第 1 張工作表
  9.             If .QueryTables.Count = 0 Then
  10.                 With .QueryTables.Add(Connection:=URL, Destination:=.Range("$A$1"))
  11.                     .Refresh BackgroundQuery:=False
  12.                 End With
  13.             End If
  14.             For E = 1339 To 2330
  15. ER:
  16.                 With .QueryTables(1)
  17.                     If Msg = False Then
  18.                       .Connection = URL & E
  19.                     ElseIf Msg Then
  20.                                   'https://djinfo.cathaysec.com.tw/z/zc/zcq/zcq0_1339_ACC.djhtm   損益表(累計季表)
  21.                        .Connection = "URL;https://djinfo.cathaysec.com.tw/z/zc/zcq/zcq0_" & E & "_ACC.djhtm"
  22.                     End If
  23.                     .PreserveFormatting = True
  24.                     .BackgroundQuery = True
  25.                     .RefreshStyle = xlInsertDeleteCells
  26.                     .SaveData = True
  27.                     .AdjustColumnWidth = True
  28.                     .RefreshPeriod = 0
  29.                     .WebSelectionType = xlSpecifiedTables
  30.                     .WebFormatting = xlWebFormattingNone
  31.                     .WebTables = "3"
  32.                     .WebPreFormattedTextToColumns = True
  33.                     .WebConsecutiveDelimitersAsOne = True
  34.                     .Refresh BackgroundQuery:=False
  35.                 End With
  36.                 If InStr(.[A3], "查無") Then Msg = True: GoTo ER
  37.                 If InStr(.[A3], "個股代碼錯誤") = 0 Then '這網頁如股票代碼錯誤會傳回負號.
  38.                      xFile = xPath & "\" & E & "\ISQ.txt"
  39.                     MkDir_Sub xFile       '10#的程式 'C槽下的季損益表資料夾不需先建立
  40.                     Maketxt xFile, .QueryTables(1)
  41.                 End If
  42.                 Msg = False
  43.             Next
  44.         End With
  45.     End With
  46. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 14# smart3135
註解有在看嗎? 要複製第10#的 MkDir_Sub 程式到模組上.
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

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

回復 17# smart3135
10# 的程式碼看不懂,已加註說明了.
會出現記憶體不足的視窗,在XP,記憶體1GB,2003版,執行5分鐘內可完成,擴充你的記憶體試試看
全部直接貼到txt中,這樣應該就不用走迴圈,慢慢再研究.
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 19# smart3135
你用的是 https://djinfo.cathaysec.com.tw/z/zc/zcq/zcqa/zcqa.djhtm
可修改如下
  1. End With
  2.                
  3.                 If InStr(.[A3], "查無") And Msg = True Then GoTo xlNext
  4.                 If InStr(.[A3], "查無") Then Msg = True: GoTo ER
  5.                 If InStr(.[A3], "個股代碼錯誤") = False Then '這網頁如股票代碼錯誤會傳回負號.
  6.                     xFile = xPath & "\" & E & "\IS.txt"
  7.                     MkDir_Sub xFile       '10#的程式 'C槽下的季損益表資料夾不需先建立
  8.                     Maketxt xFile, .QueryTables(1)
  9.                 End If
  10. xlNext:
  11.                 Msg = False
  12.             Next
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2014-4-25 12:47 編輯

回復 22# smart3135
  1. End With               
  2.                 If E = 1420 Then GoTo xlNext   '加上試試看
  3.             'If InStr(.[A3], "查無") And Msg = True Or E = 1420 Then GoTo xlNext  '或者可這樣寫
  4.             If InStr(.[A3], "查無") Then Msg = True: GoTo ER
  5.                 If InStr(.[A3], "個股代碼錯誤") = False Then '這網頁如股票代碼錯誤會傳回負號.
  6.                     xFile = XPath & "\" & E & "\IS.txt"
  7.                     MkDir_Sub xFile       '10#的程式 'C槽下的季損益表資料夾不需先建立
  8.                     Maketxt xFile, .QueryTables(1)
  9.                 End If
  10. xlNext:
  11.                 Msg = False
  12.             Next
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 24# smart3135
1420 key錯成 2149 可簡化不需 If E = 1420 Then GoTo xlNext
  1. Rows(1).Delete
  2.                 Columns(1).Delete
  3.             AR = Array(1420, 1580, 2000)  '你是要一個一個找出的
  4.             For E = 1101 To 3000
  5.                 X = Application.Match(E, AR, 0)
  6.                 If IsNumeric(X) Then GoTo xlNext  '直接到 xlNext行
  7.                                                  ' 'If E = 1420 Then GoTo xlNext   '不需要
  8. ER:
  9.                 With .QueryTables(1)
  10.                
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

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