2

最近、Python でオブジェクト指向を少し学びました。VBA でも同じことをしようとしています。

子オブジェクトの辞書であるフックを含む親オブジェクト (PC) を構築することに成功しました。Hooks は、子のディクショナリである行を持つオブジェクトでもあります。

私が書きたいのは、次のことだけです。

for each hook in PC
    for each row in hook
        sheets("X").cells(i,1) = contract.price
    next row
next hook

私はこれを見ていますが、それを機能させることはできません...

ここでクラスの概要: クラス PC

Option Explicit

Public pPC As Object
Private pName As String
Private pInclude As Boolean

Private Sub Class_Initialize()
    Set pPC = CreateObject("Scripting.Dictionary")
End Sub

Private Sub Class_Terminate()
    Set pPC = Nothing
End Sub

Public Property Get hook(HookName As String) As CHook:
     Set hook = pPC(HookName)
End Property

Public Sub Add(hook As CHook):
    If Not pPC.exists(hook.Name) Then pPC.Add hook.Name, hook
End Sub

Public Property Get Include(HookName As String) As Boolean:
    pInclude = pPC.exists(HookName)
    Include = pInclude
End Property

Public Property Let Name(pcname As String):
    pName = pcname
End Property

Public Property Get Name() As String:
    Name = pName
End Property

クラスフック

 Option Explicit

 Public pHook As Object
 Private pName As String
 Private pLTFlatPrice As Double
 Private pLTBasisPrice As Double
 Private pLTDate As Date

 Private Sub Class_Initialize()
    Set pHook = CreateObject("Scripting.Dictionary")
    pLTDate = Sheets("Control").Cells(2, 2)
 End Sub

 Private Sub Class_Terminate()
    Set pHook = Nothing
 End Sub

 Public Sub AddRow(Row As CRow)
    If Not pHook.exists(Row.ContractLot) Then pHook.Add Row.ContractLot, Row
    If Row.TradeDate < pLTDate Then
        pLTDate = Row.TradeDate
        If IsNumeric(Row.FlatMV) And Row.FlatMV <> 0 Then pLTFlatPrice = Row.FlatMV
        If IsNumeric(Row.BasisMV) Then pLTBasisPrice = Row.BasisMV
    End If
 End Sub

 Public Property Get Row(ContractLot As String) As CRow:
    Set Row = pHook.Item(ContractLot)
 End Property

 Public Property Let Name(HookName As String):
    pName = HookName
 End Property

 Public Property Get Name() As String:
    Name = pName
 End Property

 Public Property Get LTFlatPrice() As Double:
    LTFlatPrice = pLTFlatPrice
 End Property

 Public Property Get LTBasisPrice() As Double:
    LTBasisPrice = pLTBasisPrice
 End Property

 Public Property Get LTDate() As Double:
    LTDate = pLTDate
 End Property

エラーが発生した場所のコードは次のとおりです (オブジェクトはこのプロパティまたはメソッドをサポートしていません)。

i = 2 の場合 UBound(path, 1) へ

tName = パス(i, 1)

次は

PC = SArray.PC(tName) を設定します。

   For Each hook In PC

       For Each row In hook

            With Sheets("COB")

               .Cells(ii, 2) = row.PC

               .Cells(ii, 3) = row.hook

               .Cells(ii, 4) = row.Period

            End With

       ii = ii + 1

       Next row

次のフック

4

1 に答える 1

2

ディクショナリのキーまたはアイテムのいずれかを反復処理できます。

Sub Tester()

Dim d As New Scripting.Dictionary
Dim k

    d.Add "one", 1
    d.Add "two", 2
    d.Add "three", 3

    For Each k In d.Keys
        Debug.Print k
    Next

    For Each k In d.Items
        Debug.Print k
    Next

End Sub 

したがって、辞書をオブジェクトのプロパティとして公開し、それを反復処理できます。ただし、指定する必要があることを意味します.Items(デフォルトでキーになるためです。

于 2013-06-18T19:37:52.270 に答える