我通过网络查询获取货币汇率。我面临的问题是,如果我想运行循环超过20次然后它被绞死,因为我是VBA的新手,所以我需要一些帮助如何提高执行速度。 以下是我的代码。
Sub CurrencyConvert()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim Fcurrency As String, Scurrency As String
Dim i As Integer
Static k As Integer
Worksheets.Add.Name = "Temp"
k = First.Cells(Rows.Count, 3).End(xlUp).Row
For i = k + 1 To k + 16
Fcurrency = First.Cells(i, 1)
Scurrency = First.Cells(i, 2)
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.xe.com/currencyconverter/convert/?Amount=1&From=" & Fcurrency & "&To=" & Scurrency, _
Destination:=Worksheets("Temp").Range("$A$1"))
.Name = "?Amount=1&From=" & Fcurrency & "&To=" & Scurrency
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
ActiveSheet.Cells(1, 4) = VBA.Left(ActiveSheet.Cells(1, 3), VBA.InStr(1, ActiveSheet.Cells(1, 3), " "))
ActiveSheet.Cells(1, 4).Copy First.Cells(i, 3)
Worksheets("Temp").UsedRange.Clear
First.Cells(1, 5) = "Total Converted:-" & k
Next i
Worksheets("Temp").Delete
Application.ScreenUpdating = True
MsgBox "Done"
End Sub
这是我的表单格式。
CUR1 CUR2 Current Rates
USD USD 1
USD INR 61.9169
USD GBP 0.604447
USD EUR 0.721379
USD AED 3.6728
USD JOD 0.7079
USD MXN 13.1101
USD ARS 6.473
答案 0 :(得分:0)
看起来您正在为每一行运行查询服务器。 新查询中存在大量开销,对于相对少量的信息(20行左右),您应该运行包含所有汇率的查询,并使用记录集对象迭代每个速率,那么输出到excel之后。