【问题标题】:iterate custom dictionary object迭代自定义字典对象
【发布时间】:2013-06-18 14:11:48
【问题描述】:

最近在 Python 中学习了一点面向对象,我正在尝试在 VBA 中做同样的事情。

我设法构造了一个包含子对象字典的父对象 (PC):钩子。 Hooks 也是一个带有子字典的对象:rows。

所有我想做的就是能够写:

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

我正在寻找at this,但无法让它工作......

这里是类的总结: 个人电脑类

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

这是发生错误的代码的和平(对象不支持此属性或方法):

For i = 2 To UBound(path, 1)

tName = path(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

下一个钩子

【问题讨论】:

  • 为什么要在 VBA 中执行此操作?像这样的东西在 VB.Net 或 C# 中更容易做和学习。
  • 在什么情况下不工作?
  • @RBarryYoung。很好的问题。两个原因:1)我需要与 excel 完全集成,并且找不到至少使用 Python 的直接方法(我选择的代码)。 2) 确实是我为之工作的人更喜欢 VBA,因为这是他们习惯的东西。我不是后者维护代码的人。任何关于简单界面 Python X Excel 的提示都非常受欢迎。
  • @AMariani 抱歉,我无法在 Python 方面为您提供帮助。您可以通过 VSTO 在包括 Excel 在内的 MS Office 对象模型上使用 .Net 的东西(我猜想是 Iron Python)。作为一名顾问,出于站点可移植性的原因,我在所有 Excel 工作中都坚持使用 VBA,但是,我鼓励您与您的雇主讨论改用 VB.Net/VSTO 的可能性。这确实是一个非常更好的开发环境。
  • @TimWilliams。添加了带有错误 Tim 的代码。错误就在:对于 PC 中的每个钩子(对象不支持此属性或方法)。我知道我需要为循环编写方法,只是不知道如何!

标签: vba loops object excel dictionary


【解决方案1】:

您可以遍历字典的键或项:

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(因为它将默认为键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 2014-07-29
    • 2014-03-07
    • 1970-01-01
    • 2012-02-19
    相关资源
    最近更新 更多