在vba中使用模块类

时间:2015-02-19 09:23:59

标签: excel vba excel-vba

我有一个模块类文件" CombinaisonLine"在我的类模块文件夹中:

Private pAdult As String
Private pChild As String
Public Property Get Adult() As String
    Adult = pAdult
End Property
Public Property Let Adult(Value As String)
    pAdult = Value
End Property

Public Property Get Child() As String
    Child = pChild
End Property
Public Property Let Child(Value As String)
    pChild = Value
End Property

在我的modules文件夹中,当我点击工作表中的按钮时,我调用了一个函数:

Function Test()
Dim Line As CombinaisonLine   
If (Sheets("Feuil1").Cells(3, 6).Value = "YES") Then
        Line.Adult = "1"
        Line.Child = "0"            
End If
End Function

我在第34行收到错误91; Line.Adult =" 1""以下消息(我正在使用法语版本,所以我已将消息翻译成英文):

execution error "91":
Object variable or Bloc variable With not defined

我不知道自己错过了什么。在此先感谢您的帮助

1 个答案:

答案 0 :(得分:3)

首先需要创建类CombinaisonLine的对象,并在不需要时进行销毁:

Function Test()
Dim Line As CombinaisonLine 

Set Line = New CombinaisonLine

If (Sheets("Feuil1").Cells(3, 6).Value = "YES") Then
        Line.Adult = "1"
        Line.Child = "0"            
End If

Set Line = Nothing

End Function