- 帖子
- 27
- 主題
- 7
- 精華
- 0
- 積分
- 38
- 點名
- 0
- 作業系統
- Win 7
- 軟體版本
- Office 2007
- 閱讀權限
- 10
- 性別
- 男
- 註冊時間
- 2012-9-19
- 最後登錄
- 2014-2-18
|
5#
發表於 2014-2-6 11:52
| 只看該作者
回復 4# GBKEE
GBKEE版主您好
我加入您修改的程式,詳如附件
測試之後發現幾個問題
1. 尚未呼叫Delete_QueryTables前,速度差異甚大
不另存新檔:99筆資料共112秒
另存新檔:第1筆就花了121秒...
可見拖累速度是另存新檔的原因,請問有何解決之道?
2. 呼叫Delete_QueryTables,發生錯誤1004
錯誤內容如下圖
我發現錯誤內容是Q.name的地方成為"zc3_2002.djhtm_231"
不懂為何後面會多了_231這幾個字
VBA測試專用.zip (270.05 KB)
- Private Sub CommandButton1_Click()
- Dim StartTime
- StartTime = Timer
- Application.ScreenUpdating = False
- LastRow = Sheets("sheet1").Range("A2").End(xlDown).Row '找出最後一筆資料
- For i = 2 To LastRow
-
- Dim stockid
- stockid = Range("A" & i).Value
-
- Application.Wait Now + TimeValue("00:00:01")
- With Sheets("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 Sheets("ISQ")
- '.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 Sheets("BSQ")
- '.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
-
- Application.DisplayAlerts = False '存檔時直接覆蓋原有檔案
-
- Sheets(Array("CFSQ", "ISQ", "BSQ")).Copy
- ActiveWorkbook.SaveAs Filename:= _
- "C:\Users\user\Desktop\Test\" & stockid & "季報.xlsx", FileFormat:= _
- xlOpenXMLWorkbook, CreateBackup:=False
- Windows(stockid & "季報.xlsx").Close
-
- Application.DisplayAlerts = True
-
- DELETE_QueryTables
-
- Next i
- Application.ScreenUpdating = True
- EndTime = Timer
- MsgBox "本次下載共花費:" & EndTime - StartTime & "秒"
-
-
- End Sub
複製代碼 3. 程式碼加入之後,BSQ這個sheet內容會變成空白
另存新檔時,有的檔案有內容,有的檔案沒內容,這是什麼原因造成的?
4. Delete_Querytables在每一次迴圈執行完畢後呼叫對嗎?
謝謝版主回覆~ |
|