我的文件中有数据:
TIME|DIRECTION|ORDERID|SYMBOL|ROUTE||QUANTITY|PRICE
2012-05-29 11:17:18|B|1337|ZNGA|ARCA||6500|5.48
2012-05-29 11:17:53|B|1389|AAPL|ARCA||200|569.85
2012-05-29 11:18:22|S|1970|BRK/B|ARCA||600|77.48
2012-05-29 11:18:24|B|2335|BRK/B|ARCA||600|79.3
2012-05-29 11:19:27|B|2416|ZNGA|ARCA||4000|6.08
2012-05-29 11:19:29|S|2997|LNKD|ARCA||500|91.27
2012-05-29 11:19:40|S|3078|ZNGA|ARCA||3900|6.04
我正在尝试编写VBA代码,该代码将使用数据并基于代码(累积数量,价格等)返回统计信息。我已经使用字典对象和类对象编写了代码。这是一项任务,所以我必须那样做。
如果我只运行代码,则可以正常编译。数字不正确(试图解决该问题),但是代码可以编译。
当我添加一个断点并尝试进入代码时,它开始做一些有趣的事情。它在最初的几次运行中运行良好,但是当我停止运行/添加东西等操作时它开始中断。如果我进入代码,似乎认为字典对象存在。因此,语句If Not trades.exists(ticker) Then
的值为False
。然后,它大概在尝试搜索值但找不到任何值时,在行trades(ticker).cashVal = trades(ticker).cashVal - lineItems(posQuantity) * lineItems(posPrice)
上中断。
错误是:运行时错误'424':必需对象。
再次,奇怪的是,如果我在没有断点的情况下运行它,就很好。
Sub defineVar()
myFile = Range("$A$2")
End Sub
Public Sub test_everything()
Call defineVar
'Dim line As trade
Dim lines As Collection
Set lines = New Collection
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, file_line
lines.Add file_line
Loop
Close #1
Dim posTime As Byte
Dim posDirection As Byte
Dim posSymbol As Byte
Dim posQuantity As Byte
Dim posPrice As Byte
posTime = getColPosByHeader("TIME", lines(1))
posDirection = getColPosByHeader("DIRECTION", lines(1))
posSymbol = getColPosByHeader("SYMBOL", lines(1))
posQuantity = getColPosByHeader("QUANTITY", lines(1))
posPrice = getColPosByHeader("PRICE", lines(1))
Dim pos As Long
Dim ticker As String
Dim trades As Scripting.Dictionary
Set trades = New Scripting.Dictionary
Dim trade As Stock
Set trade = New Stock
pos = 2
While pos <= lines.Count
lineItems = Split(lines(pos), "|")
ticker = lineItems(posSymbol)
If Not trades.exists(ticker) Then
trade.symbol = ticker
trade.cashVal = 0
trade.positionVal = lineItems(posQuantity)
trade.endPriceVal = lineItems(posPrice)
trade.maxDateVal = lineItems(posTime)
trades.Add trade.symbol, trade
If InStr(1, lineItems(posDirection), "B") Then
trades(ticker).cashVal = trades(ticker).cashVal - lineItems(posQuantity) * lineItems(posPrice)
Else: trades(ticker).cashVal = trades(ticker).cashVal + lineItems(posQuantity) * lineItems(posPrice)
End If
Else:
If InStr(1, lineItems(posDirection), "B") Then
trades(ticker).cashVal = trades(ticker).cashVal - lineItems(posQuantity) * lineItems(posPrice)
trades(ticker).positionVal = trades(ticker).positionVal + lineItems(posQuantity)
If lineItems(posTime) >= trades(ticker).maxDateVal Then
trades(ticker).maxDateVal = lineItems(posTime)
trades(ticker).endPriceVal = lineItems(posPrice)
End If
Else: trades(ticker).cashVal = trades(ticker).cashVal + lineItems(posQuantity) * lineItems(posPrice)
trades(ticker).positionVal = trades(ticker).positionVal - lineItems(posQuantity)
If lineItems(posTime) >= trades(ticker).maxDateVal Then
trades(ticker).maxDateVal = lineItems(posTime)
trades(ticker).endPriceVal = lineItems(posPrice)
End If
End If
End If
Debug.Print trades(ticker).positionVal
pos = pos + 1
Wend
Dim k As Variant
For Each k In trades.Keys
Debug.Print k, trades(k).cashVal, trades(k).positionVal, trades(k).endPriceVal, trades(k).maxDateVal
Next
End Sub
编辑后添加: 这是我的类对象的样子:
'Class Module: Stock
Public symbol As String
Public cashVal As Double
Public positionVal As Double
Public endPriceVal As Double
Public maxDateVal As Date
有人知道如何解决这个奇怪的问题吗?