【发布时间】:2019-02-02 22:15:19
【问题描述】:
我正在使用一个 Excel 模型来说明养老金的生命周期。此特定模型可能需要 10 万笔交易,其中一笔交易被定义为余额变动,就像银行账户交易一样。
我可以使用字典脚本对象来存储余额,而且效果很好。但是,我想以最有效的方式存储各个交易,并在我的计算完成后将它们写入工作表。
为此,我再次使用了带有类模块项目的字典(我认为如果需要在以后的版本中参考它可能会很方便),但是一旦完成,将所有信息写入工作表非常慢“每个”循环。
使用带有保留的数组会更好还是有更好的选择?可以定义一个大的说,100,000 * x,如果我达到界限,则增加大小。
我之前使用了一个字符串字典键(由很多静态信息组成)和一个单元移动字典项,但这又有点慢。任何建议将不胜感激。
谢谢
Public Sub pb_sbSaveToUnitTransactionDict( _
lgCaseId As Long, _
lgEventId As Long, _
strTransactionType As String, _
lgInvCovSeqNo As Long, _
lgInvCovSegNo As Long, _
lgBasketId As Long, _
lgTransactionMonth As Long, _
dblPremIndexRate As Double, _
strUnitFundType As String, _
dblFundGrowthRate As Double, _
strUnitFundCode As String, _
dblUnitFundPrice As Double, _
dblUnitMovement As Double, _
dblUnitMovNomValue As Double, _
strTransactionBasisCode As String, _
dblTransactionAmount As Double, _
dblTransactionPercentage As Double, _
dblTotalNomSplit As Double, _
dblKeyNomSplit As Double)
'The purpose of this function is to save a unit transaction record to the unit
'transaction dictionary.
'Key: TRANSACTION ID
'Item: Class Module containing the following information
'CASE_ID
'EVENT_ID
'TRANSACTION_ID
'TRANSACTION_TYPE
'INV_COV_SEQ_NO
'INV_COV_SEG_NO
'BASKET_ID
'TRANSACTION_MONTH
'PREM_INDEX_RATE
'UNIT_FUND_TYPE
'GROWTH_RATE
'UNIT_FUND_PRICE
'UNIT_MOVEMENT
'UNIT_MOV_NOM_VALUE
'TRANS_NOM_VALUE
'TOT_NOM_SPLIT
'KEY_NOM_SPLIT
'Version Control
'Version Description Author Date
'0.14 Creation of Policy Anniversary and A.King 23/08/2018
' Guarantee Maturity Events
Dim vntTransactionId As Variant
Dim lgEndRow As Long
Dim strSheetName As String
'Set up the Dictionary Item as a Class Module
Set clsMdlUnitTransactionItem = New clsUnitTransaction
'Get the end Row in the in the Unit Transaction Table
strSheetName = "Unit Transaction"
lgNextRow = Worksheets(strSheetName).Range("A8").End(xlDown).row
'Get the next transaction ID
vntTransactionId = pb_objUnitTransaction.Count
vntTransactionId = vntTransactionId + 1 + lgNextRow - gblConst_OuputStartRow
'Populate the Unit Transaction Dictionary Class Module Item
clsMdlUnitTransactionItem.lgCaseId = lgCaseId
clsMdlUnitTransactionItem.lgEventId = lgEventId
clsMdlUnitTransactionItem.lgTransactionId = vntTransactionId
clsMdlUnitTransactionItem.strTransactionType = strTransactionType
clsMdlUnitTransactionItem.lgInvCovSeqNo = lgInvCovSeqNo
clsMdlUnitTransactionItem.lgInvCovSegNo = lgInvCovSegNo
clsMdlUnitTransactionItem.lgBasketId = lgBasketId
clsMdlUnitTransactionItem.lgTransactionMonth = lgTransactionMonth
clsMdlUnitTransactionItem.dblPremIndexRate = dblPremIndexRate
clsMdlUnitTransactionItem.strUnitFundType = strUnitFundType
clsMdlUnitTransactionItem.dblFundGrowthRate = dblFundGrowthRate
clsMdlUnitTransactionItem.strUnitFundCode = strUnitFundCode
clsMdlUnitTransactionItem.dblUnitFundPrice = dblUnitFundPrice
clsMdlUnitTransactionItem.dblUnitMovement = dblUnitMovement
clsMdlUnitTransactionItem.dblUnitMovNomValue = dblUnitMovNomValue
clsMdlUnitTransactionItem.dblTotalNomSplit = dblTotalNomSplit
clsMdlUnitTransactionItem.dblKeyNomSplit = dblKeyNomSplit
clsMdlUnitTransactionItem.strTransactionBasisCode = strTransactionBasisCode
clsMdlUnitTransactionItem.dblTransactionAmount = dblTransactionAmount
clsMdlUnitTransactionItem.dblTransactionPercentage = dblTransactionPercentage
'Add to the Unit Transaction Dictionary
pb_objUnitTransaction.Add _
key:=vntTransactionId, _
item:=clsMdlUnitTransactionItem
End Sub
Public Sub pb_sbUnloadUnitTransactionDict()
'The purpose of this sub routine is to unload all unit transactions from the
'unit transaction dictionary that are no longer required to be stored in temporary
'memmory
'This routine will be called in the processing of a Policy Anniversary
'and Guarantee Maturity Investment Events
Dim lgPolicyYear
Dim dtmPolicyYearStartDate As Date
Dim dtmPOlicyYearEndDate As Date
Dim vntTransactionId As Variant
Dim strSheetName As String
Dim lgNextRow As Long
'Get the next row on the Unit Transaction Table
strSheetName = "Unit Transaction"
lgNextRow = Worksheets(strSheetName).Range("A8").End(xlDown).row
'Loop through each of the unit transactions during this policy year and
'populate the unit transaction table
For Each vntTransactionId In pb_objUnitTransaction
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 1).Value = pb_objUnitTransaction(vntTransactionId).lgCaseId
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 2).Value = pb_objUnitTransaction(vntTransactionId).lgEventId
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 3).Value = pb_objUnitTransaction(vntTransactionId).lgTransactionId
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 4).Value = pb_objUnitTransaction(vntTransactionId).strTransactionType
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 5).Value = pb_objUnitTransaction(vntTransactionId).lgInvCovSeqNo
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 6).Value = pb_objUnitTransaction(vntTransactionId).lgInvCovSegNo
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 7).Value = pb_objUnitTransaction(vntTransactionId).lgBasketId
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 8).Value = pb_objUnitTransaction(vntTransactionId).lgTransactionMonth
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 9).Value = pb_objUnitTransaction(vntTransactionId).dblPremIndexRate
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 10).Value = pb_objUnitTransaction(vntTransactionId).strUnitFundType
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 11).Value = pb_objUnitTransaction(vntTransactionId).dblFundGrowthRate
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 12).Value = pb_objUnitTransaction(vntTransactionId).strUnitFundCode
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 13).Value = pb_objUnitTransaction(vntTransactionId).dblUnitFundPrice
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 14).Value = pb_objUnitTransaction(vntTransactionId).dblUnitMovement
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 15).Value = pb_objUnitTransaction(vntTransactionId).dblUnitMovNomValue
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 16).Value = pb_objUnitTransaction(vntTransactionId).strTransactionBasisCode
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 17).Value = pb_objUnitTransaction(vntTransactionId).dblTransactionAmount
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 18).Value = pb_objUnitTransaction(vntTransactionId).dblTransactionPercentage
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 19).Value = pb_objUnitTransaction(vntTransactionId).dblTotalNomSplit
ThisWorkbook.Sheets(strSheetName).Cells(lgNextRow + vntTransactionId, 20).Value = pb_objUnitTransaction(vntTransactionId).dblKeyNomSplit
Next vntTransactionId
'Remove all records
pb_objUnitTransaction.RemoveAll
End Sub
【问题讨论】:
-
慢速位正在逐个单元格地引用工作表。两个选项:1)在您的班级中,将数据提取到二维数组中,然后在一次操作中将其放在工作表上。 2) 利用字典的
.items属性,可能带有转置 -
嗨,克里斯,我选择了选项 1,创建了一个足够大的数组,足以容纳我可能需要的最大行数,每个循环通过字典填充,然后复制整个数组,这可以节省大量时间。感谢您的帮助
标签: arrays excel vba performance dictionary