user2443476
user2443476

Reputation: 1975

use module class in vba

I have a module class file "CombinaisonLine" in my class modules folder :

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

In my modules folder, I have a function called when I click on a button in my sheet :

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

I am getting an error 91 at line "Line.Adult="1"" with the following message (I am using a french version, so I have translated the message into english):

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

I don't know what I am missing. Thanks in advance for your help

Upvotes: 2

Views: 104

Answers (1)

BrakNicku
BrakNicku

Reputation: 5990

You need to create object of class CombinaisonLine first, and destroy when you do not need it:

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

Upvotes: 3

Related Questions