【发布时间】:2015-04-10 18:32:35
【问题描述】:
使用 Access 2010,我正在收集信息并将其放到 Excel 电子表格中。当我运行下面的代码时,我得到了
运行时错误“91”:
对象变量或未设置块
在我的课堂上这一行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 子填充我的 ChartColors,因为我试图存储一组固定的颜色,而我之前的代码正在收集数据并以更“正常”的方式将其插入到类中。
【问题讨论】:
-
不...如何/为什么看起来不正确?这正是我本周早些时候编写另一个函数的方式。
-
抱歉没有看到所有代码,我认为它的末尾是
Set Cci = Nothing -
@StevenMartin 哪个?在
Class_Initialize或GetRGB? -
好吧,您先公开声明,然后私下声明,然后将其设置为空,因此当对象不存在时会出现 91 错误
标签: excel ms-access vba ms-access-2010