除了您的客戶對象之外,還要保留對Dictionary
中所有客戶對象的引用 - 以CustomerKey爲關鍵。當您創建新的客戶對象時,首先測試新的CustomerKey是否已經存在於字典中 - 在這種情況下,跳過創建新對象,而是向用戶顯示消息。
使用該詞典,您需要添加對MS Scripting Runtime
的引用。那麼你的僞代碼將是這樣的:
Global gDicCustomers as Dictionary
LoadCustomers()
Set gDicCustomers = New Dictionary
For each worksheet in the workbook
If the worksheet contains a named range, "CustomerKey"
If gDicCustomers.Exists(CustomerKey) Then
MsgBox "Dont screw with the customer worksheets - or at least be so decent to change the customer key, too!"
Else
Create a new customer object
Set its key equal to the value in the range "CustomerKey"
Load data from the worksheet into the object
gDicCustomers.Add CustomerKey, CustomerObject
End If
End If
Loop
End
這種方法的另一個優點是,你總是可以參考與gDicCustomers(CustomerKey)
任何客戶對象...
正確的錯誤處理? –