返回列表 上一主題 發帖

[發問] 程式執行速度越來越慢

回復 10# bulletin

請教GBKEE版主
我發現執行時
每100~200筆就會發生1004錯誤
這是因為用到大量的COPY  導致記憶體出錯嗎?

將程式改寫,將資料抓取到名為temp的工作表,依序另存到新檔
是否可以改善?

或是有可能是其他原因所造成?
  1. Private Sub CommandButton1_Click()

  2.     Dim StartTime, LastRow
  3.     StartTime = Timer
  4.     DELETE_QueryTables
  5.     Application.ScreenUpdating = False
  6.     LastRow = Sheets("sheet1").Range("A2").End(xlDown).Row '找出最後一筆資料


  7.     For i = 2 To LastRow
  8.         
  9.         Dim stockid
  10.         stockid = Range("A" & i).Value
  11.    
  12.     Application.Wait Now + TimeValue("00:00:01")

  13.        With Sheets("CFSQ")
  14.            .Rows("1:64").ClearContents
  15.            
  16.            '******************************
  17.             If .QueryTables.Count > 1 Then
  18.                 .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zc3/zc3_" & stockid & ".djhtm"
  19.             Else  '沒有QueryTable時新增
  20.                 .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zc3/zc3_" & stockid & ".djhtm", Destination:=.Range("$A$1")
  21.             End If
  22.             With .QueryTables(1)  '不再新增
  23.                 .AdjustColumnWidth = True
  24.                 .RefreshPeriod = 0
  25.                 .WebSelectionType = xlSpecifiedTables
  26.                 .WebFormatting = xlWebFormattingNone
  27.                 .WebTables = "3"
  28.                 .WebPreFormattedTextToColumns = True
  29.                 .WebConsecutiveDelimitersAsOne = True
  30.                 .Refresh BackgroundQuery:=False
  31.             End With
  32.             '******************************
  33.             
  34.        End With
  35.       
  36.        With Sheets("ISQ")
  37.            .Rows("1:64").ClearContents
  38.            
  39.            '******************************
  40.             If .QueryTables.Count > 1 Then
  41.                 .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zcq/zcq_" & stockid & ".asp.htm"
  42.             Else  '沒有QueryTable時新增
  43.                 .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zcq/zcq_" & stockid & ".asp.htm", Destination:=.Range("$A$1")
  44.             End If
  45.             With .QueryTables(1)  '不再新增
  46.                 .AdjustColumnWidth = True
  47.                 .RefreshPeriod = 0
  48.                 .WebSelectionType = xlSpecifiedTables
  49.                 .WebFormatting = xlWebFormattingNone
  50.                 .WebTables = "3"
  51.                 .WebPreFormattedTextToColumns = True
  52.                 .WebConsecutiveDelimitersAsOne = True
  53.                 .Refresh BackgroundQuery:=False
  54.             End With
  55.             '******************************
  56.             
  57.        End With
  58.       
  59.        With Sheets("BSQ")
  60.            .Rows("1:64").ClearContents
  61.            
  62.            '******************************
  63.             If .QueryTables.Count > 1 Then
  64.                 .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zcp/zcpa/zcpa_" & stockid & ".asp.htm"
  65.             Else  '沒有QueryTable時新增
  66.                 .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zcp/zcpa/zcpa_" & stockid & ".asp.htm", Destination:=.Range("$A$1")
  67.             End If
  68.             With .QueryTables(1)  '不再新增
  69.                 .AdjustColumnWidth = True
  70.                 .RefreshPeriod = 0
  71.                 .WebSelectionType = xlSpecifiedTables
  72.                 .WebFormatting = xlWebFormattingNone
  73.                 .WebTables = "3"
  74.                 .WebPreFormattedTextToColumns = True
  75.                 .WebConsecutiveDelimitersAsOne = True
  76.                 .Refresh BackgroundQuery:=False
  77.             End With
  78.             '******************************
  79.            
  80.        End With
  81.       
  82.             Application.DisplayAlerts = False '存檔時直接覆蓋原有檔案
  83.       
  84.             Sheets(Array("CFSQ", "ISQ", "BSQ")).Copy
  85.             ActiveWorkbook.SaveAs Filename:= _
  86.               "C:\Users\KT\Desktop\Test\" & stockid & "季報.xlsx", FileFormat:= _
  87.                xlOpenXMLWorkbook, CreateBackup:=False
  88.             Windows(stockid & "季報.xlsx").Close
  89.                        
  90.             Application.DisplayAlerts = True
  91.       
  92.     Next i

  93.    Application.ScreenUpdating = True
  94.    EndTime = Timer
  95.    MsgBox "本次下載共花費:" & EndTime - StartTime & "秒"
  96.       
  97.    
  98. End Sub
複製代碼

TOP

回復 10# bulletin


    web查詢跟你網路連線速度有關
當主機伺服器接受太多連線或是主機端有流量管制
對同一IP的流量進行管控時
可能就無法取得資料
學海無涯_不恥下問

TOP

回復 12# Hsieh

我也想過是流量管制的問題
多謝說明

無論如何來改寫成temp好了
當做練習也好
到時再請各位前輩指教

TOP

回復 13# bulletin

各位前輩
我又修改了一個,這次直接先開啟新的活頁簿
並將資料直接存入活頁簿中

1. 先抓資料,再複製另存新檔
100筆 --> 102秒
200筆 --> 210秒

2. 先建立新檔再抓資料存檔
100筆 --> 100秒
200筆 --> 206秒

似乎少了複製另存速度稍稍快一點
但是考量網路穩定性以及網站存取的問題
應該是沒有差

此外仍然有錯誤1004的問題, 所以沒辦法測試500筆以上或是所有上市櫃公司1400檔的速度...

錯誤會發生在複製網頁資料,存到工作表中時
不一定會在何時發生
但原則上仍留在100~200筆之間會發生
我再把延遲時間拉長,看能不能避免錯誤1004
謝謝各位前輩指教~
  1. Sub stest()

  2.     Dim StartTime, LastRow
  3.     Dim wbnew As Workbook
  4.    
  5.     DELETE_QueryTables
  6.    
  7.     StartTime = Timer
  8.     LastRow = Sheets("sheet1").Range("A2").End(xlDown).Row '找出最後一筆資料

  9.     Application.ScreenUpdating = False
  10.    
  11.     For i = 2 To 101
  12.         
  13.         Dim stockid
  14.         stockid = Range("A" & i).Value
  15.    
  16.         Application.DisplayAlerts = False
  17.         Application.Wait Now + TimeValue("00:00:01")
  18.         
  19.         
  20.         Set wbnew = Workbooks.Add() '新增活頁簿
  21.         wbnew.SaveAs Filename:="C:\Users\KT\Desktop\Test2\" & stockid & ".xlsx"  '另存新檔

  22.         With wbnew.Sheets(1) 'CFSQ
  23.            '.Rows("1:64").ClearContents
  24.            
  25.            '******************************
  26.             If .QueryTables.Count > 1 Then
  27.                 .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zc3/zc3_" & stockid & ".djhtm"
  28.             Else  '沒有QueryTable時新增
  29.                 .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zc3/zc3_" & stockid & ".djhtm", Destination:=.Range("$A$1")
  30.             End If
  31.             With .QueryTables(1)  '不再新增
  32.                 .AdjustColumnWidth = True
  33.                 .RefreshPeriod = 0
  34.                 .WebSelectionType = xlSpecifiedTables
  35.                 .WebFormatting = xlWebFormattingNone
  36.                 .WebTables = "3"
  37.                 .WebPreFormattedTextToColumns = True
  38.                 .WebConsecutiveDelimitersAsOne = True
  39.                 .Refresh BackgroundQuery:=False
  40.             End With
  41.             '******************************
  42.             
  43.        End With
  44.       
  45.     With Sheets(2)
  46.            '.Rows("1:64").ClearContents
  47.            
  48.            '******************************
  49.             If .QueryTables.Count > 1 Then
  50.                 .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zcq/zcq_" & stockid & ".asp.htm"
  51.             Else  '沒有QueryTable時新增
  52.                 .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zcq/zcq_" & stockid & ".asp.htm", Destination:=.Range("$A$1")
  53.             End If
  54.             With .QueryTables(1)  '不再新增
  55.                 .AdjustColumnWidth = True
  56.                 .RefreshPeriod = 0
  57.                 .WebSelectionType = xlSpecifiedTables
  58.                 .WebFormatting = xlWebFormattingNone
  59.                 .WebTables = "3"
  60.                 .WebPreFormattedTextToColumns = True
  61.                 .WebConsecutiveDelimitersAsOne = True
  62.                 .Refresh BackgroundQuery:=False
  63.             End With
  64.             '******************************
  65.             
  66.        End With
  67.       
  68.        With Sheets(2)
  69.            '.Rows("1:64").ClearContents
  70.            
  71.            '******************************
  72.             If .QueryTables.Count > 1 Then
  73.                 .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zcp/zcpa/zcpa_" & stockid & ".asp.htm"
  74.             Else  '沒有QueryTable時新增
  75.                 .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zcp/zcpa/zcpa_" & stockid & ".asp.htm", Destination:=.Range("$A$1")
  76.             End If
  77.             With .QueryTables(1)  '不再新增
  78.                 .AdjustColumnWidth = True
  79.                 .RefreshPeriod = 0
  80.                 .WebSelectionType = xlSpecifiedTables
  81.                 .WebFormatting = xlWebFormattingNone
  82.                 .WebTables = "3"
  83.                 .WebPreFormattedTextToColumns = True
  84.                 .WebConsecutiveDelimitersAsOne = True
  85.                 .Refresh BackgroundQuery:=False
  86.             End With
  87.             '******************************
  88.            
  89.        End With
  90.       
  91.        Windows(stockid & ".xlsx").Close savechanges:=True
  92.        Application.DisplayAlerts = True

  93.     Next i
  94.    
  95.    
  96.    Application.ScreenUpdating = True
  97.    EndTime = Timer
  98.    MsgBox "本次下載共花費:" & EndTime - StartTime & "秒"
  99.       
  100.    
  101. End Sub
複製代碼

TOP

回復 14# bulletin
我抓台灣50,花了約56秒。

            
With wbnew.Sheets(1) 'CFSQ
               '.Rows("1:64").ClearContents
               
               '******************************
                If .QueryTables.Count > 1 Then
                    .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zc3/zc3_" & stockid & ".djhtm"
                Else  '沒有QueryTable時新增
                    .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zc3/zc3_" & stockid & ".djhtm", Destination:=.Range("$A$1")
                End If
                With .QueryTables(1)  '不再新增
                    .AdjustColumnWidth = True
                    .RefreshPeriod = 0
                    .WebSelectionType = xlSpecifiedTables
                    .WebFormatting = xlWebFormattingNone
                    .WebTables = "3"
                    .WebPreFormattedTextToColumns = True
                    .WebConsecutiveDelimitersAsOne = True
                    .Refresh BackgroundQuery:=False
                End With
                '******************************
               
           End With
         
        With wbnew.Sheets(2)
               '.Rows("1:64").ClearContents
               
               '******************************
                If .QueryTables.Count > 1 Then
                    .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zcq/zcq_" & stockid & ".asp.htm"
                Else  '沒有QueryTable時新增
                    .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zcq/zcq_" & stockid & ".asp.htm", Destination:=.Range("$A$1")
                End If
                With .QueryTables(1)  '不再新增
                    .AdjustColumnWidth = True
                    .RefreshPeriod = 0
                    .WebSelectionType = xlSpecifiedTables
                    .WebFormatting = xlWebFormattingNone
                    .WebTables = "3"
                    .WebPreFormattedTextToColumns = True
                    .WebConsecutiveDelimitersAsOne = True
                    .Refresh BackgroundQuery:=False
                End With
                '******************************
               
           End With
         
           With wbnew.Sheets(3)
               '.Rows("1:64").ClearContents
               
               '******************************
                If .QueryTables.Count > 1 Then
                    .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zcp/zcpa/zcpa_" & stockid & ".asp.htm"
                Else  '沒有QueryTable時新增
                    .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zcp/zcpa/zcpa_" & stockid & ".asp.htm", Destination:=.Range("$A$1")
                End If
                With .QueryTables(1)  '不再新增
                    .AdjustColumnWidth = True
                    .RefreshPeriod = 0
                    .WebSelectionType = xlSpecifiedTables
                    .WebFormatting = xlWebFormattingNone
                    .WebTables = "3"
                    .WebPreFormattedTextToColumns = True
                    .WebConsecutiveDelimitersAsOne = True
                    .Refresh BackgroundQuery:=False
                End With
                '******************************
               
           End With

很抱歉,我是小學生,不能下載檔案,是個小屁孩!

TOP

回復 14# bulletin
  1. Sub stest()

  2.         Dim StartTime, LastRow
  3.         Dim wbnew As Workbook
  4.       
  5.         DELETE_QueryTables
  6.       
  7.         StartTime = Timer
  8.         LastRow = Sheets("sheet1").Range("A2").End(xlDown).Row '找出最後一筆資料

  9.         Application.ScreenUpdating = False
  10.       
  11.         For i = 1 To 50
  12.             
  13.             Dim stockid
  14.             If i <= 50 Then
  15.                stockid = Range("A" & i + 10).Value
  16.             Else
  17.                stockid = Range("B" & i + 10).Value
  18.             End If
  19.             Application.DisplayAlerts = False
  20.             Application.Wait Now + TimeValue("00:00:01")
  21.             
  22.             
  23.             Set wbnew = Workbooks.Add() '新增活頁簿
  24.             'wbnew.SaveAs Filename:="C:\myStock\Test\" & stockid & ".xls"  '另存新檔
  25.             wbnew.SaveAs Filename:="C:\myStock\Test\" & stockid & "季報.xls"  '另存新檔

  26.             With wbnew.Sheets(1) 'CFSQ
  27.                '.Rows("1:64").ClearContents
  28.                
  29.                '******************************
  30.                 If .QueryTables.Count > 1 Then
  31.                     .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zc3/zc3_" & stockid & ".djhtm"
  32.                 Else  '沒有QueryTable時新增
  33.                     .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zc3/zc3_" & stockid & ".djhtm", Destination:=.Range("$A$1")
  34.                 End If
  35.                 With .QueryTables(1)  '不再新增
  36.                     .AdjustColumnWidth = True
  37.                     .RefreshPeriod = 0
  38.                     .WebSelectionType = xlSpecifiedTables
  39.                     .WebFormatting = xlWebFormattingNone
  40.                     .WebTables = "3"
  41.                     .WebPreFormattedTextToColumns = True
  42.                     .WebConsecutiveDelimitersAsOne = True
  43.                     .Refresh BackgroundQuery:=False
  44.                 End With
  45.                 '******************************
  46.                
  47.            End With
  48.          
  49.         With wbnew.Sheets(2)
  50.                '.Rows("1:64").ClearContents
  51.                
  52.                '******************************
  53.                 If .QueryTables.Count > 1 Then
  54.                     .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zcq/zcq_" & stockid & ".asp.htm"
  55.                 Else  '沒有QueryTable時新增
  56.                     .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zcq/zcq_" & stockid & ".asp.htm", Destination:=.Range("$A$1")
  57.                 End If
  58.                 With .QueryTables(1)  '不再新增
  59.                     .AdjustColumnWidth = True
  60.                     .RefreshPeriod = 0
  61.                     .WebSelectionType = xlSpecifiedTables
  62.                     .WebFormatting = xlWebFormattingNone
  63.                     .WebTables = "3"
  64.                     .WebPreFormattedTextToColumns = True
  65.                     .WebConsecutiveDelimitersAsOne = True
  66.                     .Refresh BackgroundQuery:=False
  67.                 End With
  68.                 '******************************
  69.                
  70.            End With
  71.          
  72.            With wbnew.Sheets(3)
  73.                '.Rows("1:64").ClearContents
  74.                
  75.                '******************************
  76.                 If .QueryTables.Count > 1 Then
  77.                     .QueryTables(1).Connection = "URL;http://dj.mybank.com.tw/z/zc/zcp/zcpa/zcpa_" & stockid & ".asp.htm"
  78.                 Else  '沒有QueryTable時新增
  79.                     .QueryTables.Add Connection:="URL;http://dj.mybank.com.tw/z/zc/zcp/zcpa/zcpa_" & stockid & ".asp.htm", Destination:=.Range("$A$1")
  80.                 End If
  81.                 With .QueryTables(1)  '不再新增
  82.                     .AdjustColumnWidth = True
  83.                     .RefreshPeriod = 0
  84.                     .WebSelectionType = xlSpecifiedTables
  85.                     .WebFormatting = xlWebFormattingNone
  86.                     .WebTables = "3"
  87.                     .WebPreFormattedTextToColumns = True
  88.                     .WebConsecutiveDelimitersAsOne = True
  89.                     .Refresh BackgroundQuery:=False
  90.                 End With
  91.                 '******************************
  92.                
  93.            End With
  94.          
  95.            'Windows(stockid & ".xls").Close savechanges:=True
  96.            Windows(stockid & "季報.xls").Close savechanges:=True
  97.            Application.DisplayAlerts = True

  98.         Next i
  99.       
  100.       
  101.        Application.ScreenUpdating = True
  102.        EndTime = Timer
  103.        MsgBox "本次下載共花費:" & EndTime - StartTime & "秒"
  104.          
  105.       
  106. End Sub
複製代碼
很抱歉,我是小學生,不能下載檔案,是個小屁孩!

TOP

        靜思自在 : 君子如水,隨方就圓,無處不自在。
返回列表 上一主題