Excel VBA将数据写入类模块中的字典

我正在尝试将数据保存在类模块中声明的字典中。 我已经在类模块中使用过一个字典,因为在开始时组的数量和相关的数据点是未知的。 下面的代码编译,但模块类模块中的dRATIO.exists语句都返回false(但是在第一次传递类模块中的debugging语句给出正确的值,之后的错误),然后Function GetRATIO返回999.任何build议?

 'CODE IN A CLASS MODULE CALLED clsIVDATA Option Explicit Public dRATIO Public dIV ' Sub Init(RATIO As Variant, IV As Variant, KEY As String) 'Dim I As Long Dim VAL As String Dim RowKeys Dim COLKEYS Set dRATIO = CreateObject("Scripting.Dictionary") Set dIV = CreateObject("Scripting.Dictionary") dRATIO.ADD ITEM:=RATIO, KEY:=KEY dIV.ADD ITEM:=RATIO, KEY:=KEY Debug.Print dRATIO.Exists("1") Debug.Print dRATIO.ITEM("1") End Sub Function GetRATIO(KEY As String) If dRATIO.Exists(KEY) Then GetRATIO = dRATIO(KEY) Else GetRATIO = 999 'or raise an error... End If End Function Function NO_VALUES() NO_VALUES = dRATIO.COUNT End Function Function GetIV(KEY As String) If dIV.Exists(KEY) Then GetIV = dIV(KEY) Else GetIV = 999 'or raise an error... End If End Function '===================================================== 'CODE IN A NORMAL MODULE Sub tstclass() Dim RATIO() As Variant Dim IV() As Variant Dim I As Integer Dim dctSKEW As Object Set dctSKEW = CreateObject("Scripting.Dictionary") dctSKEW.ADD "APZ4", New clsIVDATA RATIO = Array(0.879, 0.843, 0.802, 0.756, 0.658) IV = Array(0.165, 0.156, 0.145, 0.136, 0.125) For I = 1 To 5 KEY = CStr(I) dctSKEW("APZ4").Init RATIO(I), IV(I), KEY Next I Debug.Print dctSKEW("APZ4").GetRATIO("1") Debug.Print dctSKEW("APZ4").GetRATIO("2") Debug.Print dctSKEW("APZ4").NO_VALUES End Sub 

你的主要问题是你正在混合初始化Dictioary对象与加载项目(每次你调用clsIVDATA.Init你正在创build一个新的空字典)。

另外,使用Array(...)生成的Array(...)是基于0的,所以您的For循环将会出错并产生意外的结果。

这是你的代码,重构解决这些和其他一些小问题

class级代码

 Option Explicit 'CODE IN A CLASS MODULE CALLED clsIVDATA Private dRATIO As Object Private dIV As Object ' Private Sub Class_Initialize() Set dRATIO = CreateObject("Scripting.Dictionary") Set dIV = CreateObject("Scripting.Dictionary") End Sub Sub Init(RATIO As Variant, IV As Variant, KEY As String) dRATIO.Add Item:=RATIO, KEY:=KEY dIV.Add Item:=RATIO, KEY:=KEY End Sub Function GetRATIO(KEY As String) If dRATIO.Exists(KEY) Then GetRATIO = dRATIO(KEY) Else GetRATIO = 999 'or raise an error... End If End Function Function NO_VALUES() NO_VALUES = dRATIO.Count End Function Function GetIV(KEY As String) If dIV.Exists(KEY) Then GetIV = dIV(KEY) Else GetIV = 999 'or raise an error... End If End Function 

模块代码

 Option Explicit '===================================================== 'CODE IN A NORMAL MODULE Sub tstclass() Dim RATIO() As Variant, KEY As String Dim IV() As Variant Dim I As Long Dim c As clsIVDATA Dim dctSKEW As Object Set dctSKEW = CreateObject("Scripting.Dictionary") dctSKEW.Add "APZ4", New clsIVDATA Set c = dctSKEW.Item("APZ4") RATIO = Array(0.879, 0.843, 0.802, 0.756, 0.658) IV = Array(0.165, 0.156, 0.145, 0.136, 0.125) For I = 0 To 4 KEY = CStr(I + 1) c.Init RATIO(I), IV(I), KEY Next I Debug.Print dctSKEW("APZ4").GetRATIO("1") Debug.Print dctSKEW("APZ4").GetRATIO("2") Debug.Print dctSKEW("APZ4").NO_VALUES End Sub