Excel VBA 问题
时间:2014-07-31
来源:互联网
大概系用web-query去import一页data, 再用vlookup做一啲calculations, 如见冇用就delete rows, 再import过新一页, 一有啱用的就自动停.....
但loop咗>1000次之后, 佢越行越慢, 个file size 由300kb 变咗2mb, 个file经开关及运行一段时间,到最后更接近要hang....
请问有冇师兄知如何解决呢? tks
[ 本帖最后由 feelingbetter 於 2014-7-14 05:47 PM 编辑 ]
作者: feelingbetter 发布时间: 2014-07-31
如果有 next 之前要 set 番做 Nothing.
作者: yaris.yrx 发布时间: 2014-07-31
Loop 入面有无 set object?
如果有 next 之前要 set 番做 Nothing.
其实个code如下(web address因工作关系略去), 请问是哪里出问题呢?tks
****************************
Dim Webbaddress As String
Dim Begindate As String
Dim Enddate As String
Dim Issuenumber As String
Dim Issuecounter As Double
Dim Endissuenumber As Double
Begindate = Range("$F$4")
Enddate = Range("$F$5")
Issuenumber = Range("$F$6")
Endissuenumber = Range("$F$7")
For Issuecounter = Issuenumber To Endissuenumber
ActiveSheet.Range("27:300").Clear
Webbaddress = "************address removed************" & Begindate & "&d=" & Enddate & "&sort=chngdn&issue=" & Issuecounter
Range("$F$11") = Issuecounter
With ActiveSheet.QueryTables.Add(Connection:= _
Webbaddress _
, Destination:=Range("A27:M27"))
.Name = "*******address removed**********"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "3,4"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Range("E20").Select
'Exit procedure if criteria satisfied
If Range("$P$26").Value > 5 Then
Exit For
End If
Next Issuecounter
****************************
作者: feelingbetter 发布时间: 2014-07-31
呢句 d data type 错哂, 又 string 又 double... 虽然 vba 会同你转, 但都唔好咁。
With ActiveSheet.QueryTables.Add(Connection:= _
Webbaddress _
, Destination:=Range("A27:M27"))
呢句野点解 output 永远系 A27:M27 ?
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "3,4"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
呢渣野应该几浪费资源, 改改佢试下

[ 本帖最后由 烟民贱过食屎9. 於 2014-7-16 12:13 AM 编辑 ]
作者: 烟民贱过食屎9. 发布时间: 2014-07-31
With Workbooks.Worksheets.QueryTables(1)
.Connection = _
"URL;address removed"
.WebTables = "3,4"
.Refresh BackgroundQuery:=False
End With
作者: yfly480 发布时间: 2014-07-31
For Issuecounter = Issuenumber To Endissuenumber
呢句 d data type 错哂, 又 string 又 double... 虽然 vba 会同你转, 但都唔好咁。
With ActiveSheet.QueryTables.Add(Connection:= _
Webbadd ...
output destination冇特别意思, 因delete后有empty space才重用位置
btw...我谂我冇set object.....但如何才能set variables to nothing/kill variables去relieve memory呢?
作者: feelingbetter 发布时间: 2014-07-31
应考虑 reuse 同一个 query table, 或开新一个之前清左旧既.
作者: yaris.yrx 发布时间: 2014-07-31
yfly480 系正解, 你个 code 不断 create 新 query table, 无 free 番之前唔用个 d.
应考虑 reuse 同一个 query table, 或开新一个之前清左旧既.
作者: feelingbetter 发布时间: 2014-07-31

作者: yaris.yrx 发布时间: 2014-07-31
作者: yfly480 发布时间: 2014-07-31
macro有录制新巨集功能, 自己玩下, 睇下create既code就知点写
至於上面师兄指用refresh代替add的code, 我modify了一下放入去但唔work(出error msg), 看书又只有很少描述refresh的用法且不太明.....
有师兄可指点一下吗?tks
作者: feelingbetter 发布时间: 2014-07-31
With ActiveSheet.QueryTables(1)
.Connection = Webbaddress
.Destination = Range("A27:M27")
.Name = "chldchg.asp?d1=2014-06-30&d=2014-07-09&sort=chngdn&issue=6409"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "3,4"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
定抑或应用add, 完了再用另一set code去delete all web query?
http://www.mrexcel.com/forum/exc ... ries-worksheet.html
作者: feelingbetter 发布时间: 2014-07-31
QTcount = ActiveSheet.QueryTables.Count
If QTcount > 0 Then
For QTIndex = 1 To QTcount
ActiveSheet.QueryTables(QTIndex).Delete
Next
End If
用了以上的code好像ok了....谢谢各位~
作者: feelingbetter 发布时间: 2014-07-31
.WebFormatting = xlWebFormattingNone
.WebTables = "3,4"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
Are these code unneccessary? will the VBA run faster without them?
作者: feelingbetter 发布时间: 2014-07-31
Good for u.

For your previous post, the last sentence is very important and cannot be removed.
Other sentences are pretty optional but shouldn't have materialized performance impact.
作者: yaris.yrx 发布时间: 2014-07-31
Glad to hear the problem is solved.
Good for u.

For your previous post, the last sentence is very important and cannot be removed.
Other sentences are pretty optional but shouldn't have mater ...
On the other hand, if I want to keep the macro running (while recording the issuenumbers with positive search results) instead of exiting the sub whenever a result shows, how can i achieve that?
I thought of using some cells (variable value=issuenumbers) to hold the result records, but as the querytable gets cleared up for the analysis of the next issuenumber, the variable value would get cleared up as well.....is there any method to get around this?
作者: feelingbetter 发布时间: 2014-07-31
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28