类函数中的错误91

时间:2015-04-10 18:32:35

标签: excel excel-vba access-vba ms-access-2010 vba

使用Access 2010,我收集信息并将其放在Excel电子表格中。当我运行下面的代码时,我正在

  

运行时错误' 91':对象变量或With block not set

Set Cci = ChartColorItems(ColorID)

中的这一行Public Function GetRGB(ByRef ColorID As String) As Integer上课

' ChartColors'类:

Option Compare Database
Option Explicit

Private pChartColorItems As Collection

Public Property Get ChartColorItems() As Collection
  Set ChartColorItems = pChartColorItems
End Property
Public Property Set ChartColorItems(ByRef lChartColorItem As Collection)
  Set pChartColorItems = lChartColorItem
End Property

Public Function GetRGB(ByRef ColorID As String) As Integer
  Dim Cci As ChartColorItem
  Dim x As Integer

'---------------------------------------------------
'Error happens here:      
  Set Cci = ChartColorItems(ColorID)
'---------------------------------------------------
  x = RGB(Cci.Red, Cci.Green, Cci.Blue)
  GetRGB = x
  Set Cci = Nothing

End Function

Private Sub Class_Initialize()

  Dim Cci As ChartColorItem
  Dim Colors As Collection

  Set Colors = New Collection

  Set Cci = New ChartColorItem
  Cci.Red = 149
  Cci.Green = 55
  Cci.Blue = 53
  Cci.ColorID = "Pie1"
  Colors.Add Cci, Key:=Cci.ColorID
  Set Cci = Nothing

  Set Cci = New ChartColorItem
  Cci.Red = 148
  Cci.Green = 138
  Cci.Blue = 84
  Cci.ColorID = "Pie2"
  Colors.Add Cci, Key:=Cci.ColorID
  Set Cci = Nothing

End Sub

和ChartColorItem类:

Option Compare Database
Option Explicit

Private pColorID As String
Private pRed As Integer
Private pGreen As Integer
Private pBlue As Integer

Public Property Get ColorID() As String
  ColorID = pColorID
End Property
Public Property Let ColorID(ByRef x As String)
  pColorID = x
End Property

Public Property Get Red() As Integer
  Red = pRed
End Property
Public Property Let Red(ByRef x As Integer)
  pRed = x
End Property

Public Property Get Green() As Integer
  Green = pGreen
End Property
Public Property Let Green(ByRef x As Integer)
  pGreen = x
End Property

Public Property Get Blue() As Integer
  Blue = pBlue
End Property
Public Property Let Blue(ByRef x As Integer)
  pBlue = x
End Property

当我调试时,代码逐步完成ChartColorItems() getter,错误发生在End Function之后,并将我放在上面提到的那条线上。

这与我本周早些时候写的一些代码非常相似,主要区别在于我使用Class_Initialize sub来填充我的ChartColors,因为我试图存储一个固定的颜色集,而我之前的代码是收集数据并将其插入更正常的类中。办法。

1 个答案:

答案 0 :(得分:2)

您在模块级别将私有集合定义为pCharColorItems,但您从未在类的初始化方法中初始化它。相反,您使用本地范围的Colors集合变量。

Private Sub Class_Initialize()

  Dim Cci As ChartColorItem
  Dim Colors As Collection

  Set Colors = New Collection

您需要改为使用pChartColorItems

Private Sub Class_Initialize()

  Dim Cci As ChartColorItem
  Dim Colors As Collection

  Set pChartColorItems = New Collection

  Set Cci = New ChartColorItem
  Cci.Red = 149
  Cci.Green = 55
  Cci.Blue = 53
  Cci.ColorID = "Pie1"
  pChartColorItems.Add Cci, Key:=Cci.ColorID
  Set Cci = Nothing

  Set Cci = New ChartColorItem
  Cci.Red = 148
  Cci.Green = 138
  Cci.Blue = 84
  Cci.ColorID = "Pie2"
  pChartColorItems.Add Cci, Key:=Cci.ColorID
  Set Cci = Nothing

End Sub

但是这行GetRGB还有另一个错误。

 x = RGB(Cci.Red, Cci.Green, Cci.Blue)

x函数返回long时,您将RGB声明为整数。 “Pie1”的值会导致溢出错误。