我正在努力获得经验。一些报价的移动平均线。我构建了一个自定义函数来在我的工作表中检索它。当我试图在单元格中执行时,它会抛出#Value Error
. 但是当我在即时窗口中执行它时,它运行得很好。即使我试图让它变得不稳定。我不确定为什么会这样。下面是我的 VBA 代码:
Option Explicit
Function ExpMovingAverage9(Symbol As String, FromDate As String, ToDate As String) As Variant
Application.Volatile
Dim ws As Worksheet
Dim tblInv As ListObject
Dim SymbolTable As ListObject
Dim FromDateTable As ListObject
Dim ToDateTable As ListObject
Dim Last As Long
Dim LastValue As Variant
Set ws = ThisWorkbook.Worksheets("AARTIIND")
Set tblInv = ws.ListObjects("ChartData")
Set SymbolTable = ws.ListObjects("Symbol")
Set FromDateTable = ws.ListObjects("FromDate")
Set ToDateTable = ws.ListObjects("ToDate")
SymbolTable.DataBodyRange.Cells(1, 1) = Symbol
FromDateTable.DataBodyRange.Cells(1, 1) = FromDate
ToDateTable.DataBodyRange.Cells(1, 1) = ToDate
tblInv.QueryTable.Refresh
Application.CalculateUntilAsyncQueriesDone
Last = tblInv.Range.Rows(tblInv.Range.Rows.Count).Row
LastValue = Range("G" & (Last - 1)).Value
ExpMovingAverage9 = LastValue
End Function