我有一个类State
,其中包含方法setStateName
(设置名称属性),storeBudgetWorkbooks
(设置和编辑字符串集合),storeBudgetDatas
(设置和编辑)一组其他对象)和editResultWorkbook
(打开另一个工作簿,并将storeBudgetDatas
中创建的对象集合中的数据放入其中。)
我有一个使用该类的主要子:
Sub StartGrabbings()
...
For stateCount = LBound(stateNames) To UBound(stateNames)
Dim stateCopy As New State
stateCopy.setStateName = stateNames(stateCount)
stateCopy.storeBudgetWorkbooks
stateCopy.storeBudgetDatas
If stateCopy.getEmptyData = False Then
Call stateCopy.editResultWorkbook(resultWorkbook, rowsCount)
rowsCount = rowsCount + 10
End If
Next
...
End Sub
但是当我打开结果工作簿时,我发现来自一个State
实例的数据被置于其他州的名称之下。我无法理解这个的原因 - 我创建了一个State类对象的新副本,创建并设置了它的内部集合和对象,然后将这些数据放在结果工作簿中。为什么我只从第一个状态获取数据(但是更新了其他状态的名称)。
然而,当我宣布这样的课程时,一切正常:
Sub StartGrabbings()
...
Dim stateCopy As State
For stateCount = LBound(stateNames) To UBound(stateNames)
Set stateCopy = New State
stateCopy.setStateName = stateNames(stateCount)
stateCopy.storeBudgetWorkbooks
stateCopy.storeBudgetDatas
If stateCopy.getEmptyData = False Then
Call stateCopy.editResultWorkbook(resultWorkbook, rowsCount)
rowsCount = rowsCount + 10
End If
Next
...
End Sub
这是我的班级State
:
Private stateName As String
Private budgetWorkbooks As Collection '2014-2017, a workbook
Private allBudgetItems As Collection '2014-2017, a collection of collections of items
Private emptyData As Boolean
Private ratingDatas As Collection
Public Property Let setStateName(value As String)
stateName = value
End Property
...
Private Sub Class_Initialize()
Set budgetWorkbooks = New Collection
Set allBudgetItems = New Collection
Set ratingDatas = New Collection
emptyData = False
End Sub
Function storeBudgetWorkbooks()
Dim year As Integer
For year = 2014 To 2017
budgetWorkbooks.Add (getBudgetWorkbook(year))
Next
End Function
Sub storeBudgetDatas()
...
Dim year As Integer
For year = 2014 To 2017
...
allBudgetItems.Add getBudgetData(budgetWorkbook, year)
...
Next
...
End Sub
Function getBudgetData(budgetWorkbook As Workbook, year As Integer)
...
Dim budgetItems As Collection
Set budgetItems = getBudgetItems(year)
Dim budgetItem As item
For Each budgetItem In budgetItems
... 'set attributes
Next
...
Set getBudgetData = budgetItems
End Function
Sub editResultWorkbook(resultWorkbook As Workbook, rowsCount As Long)
Dim i As Integer
i = 1
Dim year As Integer
For year = 2014 To 2017
Call fillBudgetResultWorkbook(resultWorkbook, allBudgetItems(i), _
rowsCount, stateName, year)
i = i + 1
Next
End Sub
Sub fillBudgetResultWorkbook(resultWorkbook As Workbook, resultCollection As Variant, _
rowsCount As Long, stateName As String, year As Integer)
...
Dim budgetItem As item
For Each budgetItem In resultCollection
resultWorkbook.Worksheets("1").Cells(i, 1).Value = stateName
resultWorkbook.Worksheets("1").Cells(i, 2).Value = budgetItem.getTitle()
resultWorkbook.Worksheets("1").Cells(i, 3).Value = budgetItem.getKind()
resultWorkbook.Worksheets("1").Cells(i, j).Value = budgetItem.getBudgetValue()
...
End Sub
看起来有些State
属性,如stateName
确实得到更新,其他人就像收集项目一样 - allBudgetItems
不...为什么会发生这种情况,为什么会这样? StartGrabbings
的第二个版本修复了它吗?它似乎来自于Dim object As New ObjectType
和Dim object As ObjectType, Set object = New ObjectType
之间的差异,我一直认为它们是相同的。