Problème avec ma collection d'objet

Résolu
youdream Messages postés 29 Date d'inscription samedi 22 octobre 2005 Statut Membre Dernière intervention 21 mars 2011 - 29 nov. 2009 à 18:51
youdream Messages postés 29 Date d'inscription samedi 22 octobre 2005 Statut Membre Dernière intervention 21 mars 2011 - 1 déc. 2009 à 14:01
bonjour à tous,

Après une heure de recherche sur CS, je ne trouve pas chaussure à mon pied...

Le principe de l'application : Lister sur la feuil1 toute les lignes des autre feuilles marqué par un "X".

Pour cela, j'ai codé ceci :

Classe Article :
Private sNom As String
Private lPrixUnitaire As Long
Private lPrixAuKilo As Long
Private iQuantiteDispo As Integer
Private sImportant As String
Private iQuantiteCommandee As Integer

Public Property Get Nom() As String
Nom = sNom
End Property

Public Property Let Nom(ByVal NewValue As String)
sNom = NewValue
End Property

Public Property Get PrixUnitaire() As Long
PrixUnitaire = lPrixUnitaire
End Property

Public Property Let PrixUnitaire(ByVal NewValue As Long)
lPrixUnitaire = NewValue
End Property

Public Property Get PrixAuKilo() As Long
PrixAuKilo = lPrixAuKilo
End Property

Public Property Let PrixAuKilo(ByVal NewValue As Long)
lPrixAuKilo = NewValue
End Property

Public Property Get QuantiteDispo() As Integer
QuantiteDispo = iQuantiteDispo
End Property

Public Property Let QuantiteDispo(ByVal NewValue As Integer)
iQuantiteDispo = NewValue
End Property

Public Property Get Important() As String
Important = sImportant
End Property

Public Property Let Important(ByVal NewValue As String)
sImportant = NewValue
End Property

Public Property Get QuantiteCommandee() As Integer
QuantiteCommandee = iQuantiteCommandee
End Property

Public Property Let QuantiteCommandee(ByVal NewValue As Integer)
iQuantiteCommandee = NewValue
End Property



Classe Collection d'Article :
Private cArticles As Collection

Public Property Get Count() As Long
Count = cArticles.Count
End Property

Public Sub AjouterArticle(ByVal NewArticle As oArticle)
cArticles.Add NewArticle
End Sub

Public Function UnArticle(ByVal Index As Integer) As oArticle
UnArticle = cArticles.Item(Index)
End Function



Code de la macro :
Public oArticles As oArticles
Public oArticle As oArticle

Public Sub CreerListe()

Set oArticle = New oArticle
Set oArticles = New oArticles

Dim Feuille As Worksheet

Dim r As Integer

For Each Feuille In Worksheets

Worksheets.Item(Feuille.Name).Select
r = 2

Do While Cells(r, "B") <> ""

If Cells(r, "F") = "X" Then

With oArticle
.Nom = Cells(r, "B")
.PrixUnitaire = Cells(r, "C")
.PrixAuKilo = Cells(r, "D")
.QuantiteDispo = Cells(r, "E")
.Important = Cells(r, "F")
.QuantiteCommandee = Cells(r, "G")
End With

If Not (oArticle Is Nothing) Then

oArticles.AjouterArticle oArticle

End If

End If

r = r + 1

Loop

Next

Worksheets.Item("Liste").Select

For i = 1 To oArticles.Count

With oArticles.UnArticle(i)
Cells(i + 1, "B").Value = .Nom
Cells(i + 1, "C").Value = .PrixUnitaire
Cells(i + 1, "D").Value = .PrixAuKilo
Cells(i + 1, "E").Value = .QuantiteDispo
Cells(i + 1, "F").Value = .Important
Cells(i + 1, "G").Value = .QuantiteCommandee
End With

Next i

End Sub


Le problème : l'erreur "Variable objet ou variable bloc With non définie (erreur 91)" s'affiche à l'exécution de "With oArticles.UnArticle(i)"

Je ne trouve pas la cause de ce problème.
Quelqu'un peut-il éclairer ma lanterne ?

Merci

2 réponses

Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 75
1 déc. 2009 à 08:53
bon, déjà, simlifions le code de la classe Article pour avoir :

Public Nom As String
Public PrixUnitaire As Long
Public PrixAuKilo As Long
Public QuantiteDispo As Integer
Public Important As String
Public QuantiteCommandee As Integer


Dans Articles, tu n'instancie pas ta collection.
De plus, pour affecter un objet, on utilises le mot clé Set.
Faire :

Private cArticles As New Collection
Public Property Get Count() As Long
Count = cArticles.Count
End Property

Public Sub AjouterArticle(ByRef NewArticle As oArticle)
cArticles.Add NewArticle
End Sub

Public Function UnArticle(ByVal Index As Integer) As oArticle
Set UnArticle = cArticles.Item(Index)
End Function


Enfin, dans le code de ta feuille, ce test:
If Not (oArticle Is Nothing) Then

est inutile, puisque toujours vrai.


Renfield - Admin CodeS-SourceS - MVP Visual Basic
3
youdream Messages postés 29 Date d'inscription samedi 22 octobre 2005 Statut Membre Dernière intervention 21 mars 2011
1 déc. 2009 à 14:01
Bonjour,

Merci Renfield.

Je test ca ce soir en rentrant.

J'accepterai la réponse si tout va bien.

encore merci.
0
Rejoignez-nous