Excel VBA:使用对象变量循环字典中的项
我试图用一个引用继承类“bride”的对象变量来循环字典中的项,但是我不能用字典来循环,但是对于集合,它非常简单,有没有一种方法可以不用字典的键来解决这个问题?因为那样我就失去了使用智能感知功能的能力 以下是该类品种的代码:Excel VBA:使用对象变量循环字典中的项,excel,vba,dictionary,Excel,Vba,Dictionary,我试图用一个引用继承类“bride”的对象变量来循环字典中的项,但是我不能用字典来循环,但是对于集合,它非常简单,有没有一种方法可以不用字典的键来解决这个问题?因为那样我就失去了使用智能感知功能的能力 以下是该类品种的代码: Option Explicit Public Property Get Name() As String End Property Public Property Get Color() As String End Property Public Property G
Option Explicit
Public Property Get Name() As String
End Property
Public Property Get Color() As String
End Property
Public Property Get Price() As Double
End Property
以下是分类狗的代码:
Option Explicit
Implements Breed
Private pName As String, pPrice As Double, pColor As String
Public Property Let Name(Val As String)
pName = Val
End Property
Public Property Get Name() As String
Name = pName
End Property
Private Property Get Breed_Name() As String
Breed_Name = Name
End Property
'''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Property Let Price(Val As Double)
pPrice = Val
End Property
Public Property Get Price() As Double
Price = pPrice
End Property
Private Property Get Breed_Price() As Double
Breed_Price = Price
End Property
'''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Property Let Color(Val As String)
pColor = Val
End Property
Public Property Get Color() As String
Color = pColor
End Property
Private Property Get Breed_Color() As String
Breed_Color = Color
End Property
以下是Cats类的代码:
Option Explicit
Implements Breed
Private pName As String, pPrice As Double, pColor As String
Public Property Let Name(Val As String)
pName = Val
End Property
Public Property Get Name() As String
Name = pName
End Property
Private Property Get Breed_Name() As String
Breed_Name = Name
End Property
'''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Property Let Price(Val As Double)
pPrice = Val
End Property
Public Property Get Price() As Double
Price = pPrice
End Property
Private Property Get Breed_Price() As Double
Breed_Price = Price
End Property
'''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Property Let Color(Val As String)
pColor = Val
End Property
Public Property Get Color() As String
Color = pColor
End Property
Private Property Get Breed_Color() As String
Breed_Color = Color
End Property
以下是具有集合但使用字典失败的常规模块的代码:
Option Explicit
Sub Main()
Dim C As Cats
Dim D As Dogs
Dim Coll As Collection
Dim B As Breed
Set C = New Cats
C.Name = "Catomon"
C.Color = "Angle White"
C.Price = 800.98
Set D = New Dogs
D.Name = "Dogomon"
D.Color = "Golden White"
D.Price = 1000.23
Set Coll = New Collection
Coll.Add C
Coll.Add D
Set B = New Breed
For Each B In Coll
Debug.Print B.Name, B.Color, B.Price
Next B
Set C = Nothing
Set D = Nothing
Set B = Nothing
Set Coll = Nothing
End Sub
字典方法
.Keys()
和.Items()
返回数组。迭代数组的唯一方法是使用Variant
类型的变量。有了这些限制,我能想到的唯一方法就是在循环内将变量转换为typeBreed
。这样,在演员阵容之后,你就有了智能感知
根据您发布的代码,示例如下:
Sub MainWithDictionary()
Dim C As Cats
Dim D As Dogs
Dim Dict As Scripting.Dictionary
Dim B As Breed
Dim K As Variant 'new variable
Set C = New Cats
C.Name = "Catomon"
C.Color = "Angle White"
C.Price = 800.98
Set D = New Dogs
D.Name = "Dogomon"
D.Color = "Golden White"
D.Price = 1000.23
Set Dict = New Scripting.Dictionary
'Keys are just placeholders
Dict.Add 1, C
Dict.Add 2, D
For Each K In Dict.Items()
'Cast the Variant result to Breed
Set B = K
'You will have Intellisense on each dictionary items after this
Debug.Print B.Name, B.Color, B.Price
Next K
Set C = Nothing
Set D = Nothing
Set B = Nothing
Set Dict = Nothing
End Sub
您想循环使用字典的
.Keys
。您好,我知道如何循环使用键,但这需要变量声明,我希望使用可以提供IntelisSense帮助的对象变量循环使用字典。谢谢您,Nuri,这很有意义。