Erreur de compilation

Résolu/Fermé
bestkeeper Messages postés 89 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 16 juillet 2020 - 13 oct. 2008 à 09:50
Polux31 Messages postés 6917 Date d'inscription mardi 25 septembre 2007 Statut Membre Dernière intervention 1 novembre 2016 - 14 oct. 2008 à 12:08
Bonjour,

j'ai une erreur dans ma macro "erreur de compilation, variable non définie" sur la ligne ReDim Preserve my tab


Voici la macro :

Sub Bouton1_QuandClic()


Private myTab()
Private ind As Long

Public Sub mainTri()

prepareTri
lanceRecap

End Sub

Private Sub prepareTri()
Dim ws As Worksheet
Dim i As Long

Set ws = Worksheets(1)
i = 2
While ws.Range("A" & i).Value <> ""
If doesExist(ws.Range("A" & i).Value) = False Then
ind = ind + 1
ReDim Preserve myTab(ind)
myTab(ind) = ws.Range("A" & i).Value
End If
i = i + 1
Wend

Set ws = Nothing

End Sub

Private Sub lanceRecap()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lig1 As Long
Dim lig2 As Long
Dim i As Long
Dim str As Variant

Set ws1 = Worksheets(1)
Set ws2 = Worksheets(2)
lig2 = 2

For i = 1 To ind
lig1 = 2
str = myTab(i)
While ws1.Range("A" & lig1).Value <> ""
If ws1.Range("A" & lig1).Value = str Then
ws1.Range("B" & lig1).Copy Destination:=ws2.Range("A" & lig2)
ws1.Range("A" & lig1).Copy Destination:=ws2.Range("B" & lig2)
ws1.Range("E" & lig1).Copy Destination:=ws2.Range("C" & lig2)
lig2 = lig2 + 1
End If
lig1 = lig1 + 1
Wend
Next i

Set ws1 = Nothing
Set ws2 = Nothing

End Sub

Private Function doesExist(ByVal str As Variant) As Boolean
Dim i As Long

For i = 1 To ind
If myTab(i) = str Then
doesExist = True
Exit Function
End If
Next i

doesExist = False

End Function

End Function

Help SVP.

Cette macro me sert à effectuer un récapitulatif de commande.

42 réponses

bestkeeper Messages postés 89 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 16 juillet 2020
13 oct. 2008 à 12:47
Désolé mais je n'y comprends rien.

Il me semble que c'est toi qui avait écrit la macro, et j'ai du faire une erreur en la recopiant.
1
Polux31 Messages postés 6917 Date d'inscription mardi 25 septembre 2007 Statut Membre Dernière intervention 1 novembre 2016 1 204
13 oct. 2008 à 12:58
Oui c'est bien moi qui ai fait ce code ... mais ça ne devait pas se présenter comme çà ...

;o)
1
Polux31 Messages postés 6917 Date d'inscription mardi 25 septembre 2007 Statut Membre Dernière intervention 1 novembre 2016 1 204
13 oct. 2008 à 13:01
Comment ça "les données de la première page" ???
1
bestkeeper Messages postés 89 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 16 juillet 2020
13 oct. 2008 à 13:05
En fait dans mon premier onglet "commande", je crée une page par commande fournisseur.

Et la macro ne prends en compte que les données de la première page.

en espérant être assez clair.
1

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
bestkeeper Messages postés 89 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 16 juillet 2020
13 oct. 2008 à 13:18
Voici ce que j'ai écrit.
Et toujours le même problème


Option Explicit

Private myTab()
Private ind As Long

Public Sub mainTri()

prepareTri
lanceRecap

End Sub

Private Sub prepareTri()
Dim ws As Worksheet
Dim i As Long

Set ws = Worksheets("commandes")
i = 2
While ws.Range("A" & i).Value <> ""
If doesExist(ws.Range("A" & i).Value) = False Then
ind = ind + 1
ReDim Preserve myTab(ind)
myTab(ind) = ws.Range("A" & i).Value
End If
i = i + 1
Wend

Set ws = Nothing

End Sub

Private Sub lanceRecap()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lig1 As Long
Dim lig2 As Long
Dim i As Long
Dim str As Variant

Set ws1 = Worksheets(1)
Set ws2 = Worksheets(2)
lig2 = 2

For i = 1 To ind
lig1 = 2
str = myTab(i)
While ws1.Range("A" & lig1).Value <> ""
If ws1.Range("A" & lig1).Value = str Then
ws1.Range("B" & lig1).Copy Destination:=ws2.Range("A" & lig2)
ws1.Range("A" & lig1).Copy Destination:=ws2.Range("B" & lig2)
ws1.Range("E" & lig1).Copy Destination:=ws2.Range("C" & lig2)
lig2 = lig2 + 1
End If
lig1 = lig1 + 1
Wend
Next i

Set ws1 = Nothing
Set ws2 = Nothing

End Sub

Private Function doesExist(ByVal str As Variant) As Boolean
Dim i As Long

For i = 1 To ind
If myTab(i) = str Then
doesExist = True
Exit Function
End If
Next i

doesExist = False

End Function
1
Polux31 Messages postés 6917 Date d'inscription mardi 25 septembre 2007 Statut Membre Dernière intervention 1 novembre 2016 1 204
13 oct. 2008 à 13:31
As-tu une erreur ? si oui quel est le message et sur quelle ligne se produit-elle ?
1
Polux31 Messages postés 6917 Date d'inscription mardi 25 septembre 2007 Statut Membre Dernière intervention 1 novembre 2016 1 204
13 oct. 2008 à 13:36
la macro recopie des données de l'onglet "commande" vers l'onglet "recap", je ne vois pas le problème.
1
bestkeeper Messages postés 89 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 16 juillet 2020
13 oct. 2008 à 14:09
peut-être des sauts de page?

J'en ai sur mon onglet commande.
1
Polux31 Messages postés 6917 Date d'inscription mardi 25 septembre 2007 Statut Membre Dernière intervention 1 novembre 2016 1 204
13 oct. 2008 à 15:12
Regarde si ça fonctionne, je n'ai pas pu tester


Private Sub lanceRecap()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lig1 As Long
Dim lig2 As Long
Dim i As Long
Dim str As Variant
 
   Set ws1 = Worksheets("commandes")
   Set ws2 = Worksheets("recap")
   lig2 = 2

   Call TriTab(True)   'True tri croissant, False tri décroissant

   For i = 1 To ind
      lig1 = 2
     str = myTab(i)
    While ws1.Range("A" & lig1).Value <> ""
       If ws1.Range("A" & lig1).Value = str Then
          ws1.Range("B" & lig1).Copy Destination:=ws2.Range("A" & lig2)
          ws1.Range("A" & lig1).Copy Destination:=ws2.Range("B" & lig2)
          ws1.Range("E" & lig1).Copy Destination:=ws2.Range("C" & lig2)
          lig2 = lig2 + 1
      End If
      lig1 = lig1 + 1
   Wend
  Next i 

Set ws1 = Worksheets("commandes")
Set ws2 = Worksheets("recap")

End Sub 

Sub TriTab(bASC As Boolean)
    Dim i As Long, j As Long
    Dim Temp As String
    
    If bASC Then    '  croissant
        For i = LBound(myTab()) To UBound(myTab()) - 1
            For j = i + 1 To UBound(myTab())
                If myTab(i) > myTab(j) Then
                    Temp = myTab(j)
                    myTab(j) = myTab(i)
                    myTab(i) = Temp
                End If
            Next j
        Next i
    Else            ' décroissant
        For i = LBound(myTab()) To UBoundmyTab()) - 1
            For j = i + 1 To UBound(myTab())
                If myTab(i) < myTab(j) Then
                    Temp = myTab(j)
                    myTab(j) = myTab(i)
                    myTab(i) = Temp
                End If
            Next j
        Next i
    End If
End Sub


;o)
0
bestkeeper Messages postés 89 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 16 juillet 2020
13 oct. 2008 à 15:37
Non.

j'ai une erreur ici

Private Sub prepareTri()
Dim ws As Worksheet
Dim i As Long

Set ws = Worksheets("commandes")
i = 2
While ws.Range("A" & i).Value <> ""
If doesExist(ws.Range("A" & i).Value) = False Then (sur le doesExist)
0
Polux31 Messages postés 6917 Date d'inscription mardi 25 septembre 2007 Statut Membre Dernière intervention 1 novembre 2016 1 204
13 oct. 2008 à 15:48
bizarre ... cette ligne n'a rien à voir avec la fonction doesExist qui fonctionnait avant ...

Ce problème vient d'ailleur.

0
bestkeeper Messages postés 89 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 16 juillet 2020
13 oct. 2008 à 15:58
Ne t'embêtes pas, je ferai un tri manuel.

Merci du temps que tu m'as consacré.

Sympa.
0
Polux31 Messages postés 6917 Date d'inscription mardi 25 septembre 2007 Statut Membre Dernière intervention 1 novembre 2016 1 204
13 oct. 2008 à 16:00
Peux tu remettre l'ensemble de ton code ici stp ?

J'aimerai comprendre pourquoi cela ne fonctionne pas ...

Merci

;o)
0
bestkeeper Messages postés 89 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 16 juillet 2020
13 oct. 2008 à 16:04
Voici l'ensemble du code.

Petite question, la macro ne fonctionne pas si j'ajoute dans ma récap une colonne dans laquelle j'ai une formule du type SI(ESTNA...
En fait cela me copie la formul et non le résultat.



Option Explicit

Private myTab()
Private ind As Long

Public Sub mainTri()

prepareTri
lanceRecap

End Sub

Private Sub prepareTri()
Dim ws As Worksheet
Dim i As Long

Set ws = Worksheets("commandes")
i = 2
While ws.Range("A" & i).Value <> ""
If doesExist(ws.Range("A" & i).Value) = False Then
ind = ind + 1
ReDim Preserve myTab(ind)
myTab(ind) = ws.Range("A" & i).Value
End If
i = i + 1
Wend

Set ws = Nothing

End Sub

Private Sub lanceRecap()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lig1 As Long
Dim lig2 As Long
Dim i As Long
Dim str As Variant

Set ws1 = Worksheets("commandes")
Set ws2 = Worksheets("recap")
lig2 = 2

Call TriTab(True) 'True tri croissant, False tri décroissant

For i = 1 To ind
lig1 = 2
str = myTab(i)
While ws1.Range("A" & lig1).Value <> ""
If ws1.Range("A" & lig1).Value = str Then
ws1.Range("B" & lig1).Copy Destination:=ws2.Range("A" & lig2)
ws1.Range("A" & lig1).Copy Destination:=ws2.Range("B" & lig2)
ws1.Range("E" & lig1).Copy Destination:=ws2.Range("C" & lig2)
lig2 = lig2 + 1
End If
lig1 = lig1 + 1
Wend
Next i

Set ws1 = Worksheets("commandes")
Set ws2 = Worksheets("recap")

End Sub

Sub TriTab(bASC As Boolean)
Dim i As Long, j As Long
Dim Temp As String

If bASC Then ' croissant
For i = LBound(myTab()) To UBound(myTab()) - 1
For j = i + 1 To UBound(myTab())
If myTab(i) > myTab(j) Then
Temp = myTab(j)
myTab(j) = myTab(i)
myTab(i) = Temp
End If
Next j
Next i
Else ' décroissant
For i = LBound(myTab()) To UBoundmyTab()) - 1
For j = i + 1 To UBound(myTab())
If myTab(i) < myTab(j) Then
Temp = myTab(j)
myTab(j) = myTab(i)
myTab(i) = Temp
End If
Next j
Next i
End If
End Sub
0
Polux31 Messages postés 6917 Date d'inscription mardi 25 septembre 2007 Statut Membre Dernière intervention 1 novembre 2016 1 204
13 oct. 2008 à 16:28
J'ai fait une petite erreur en bas de la Sub LanceRecap(), il faut remplacer :

Set ws1 = Worksheets("commandes")
Set ws2 = Worksheets("recap")

par

Set ws1 = Nothing
Set ws2 = Nothing

mais ça n'a aucune influence sur le code.

Je ne vois pas d'autres erreurs suceptibles de l'empêcher de tourner.

Quant à la recopie de la formule à la place du résultat, je ne sais pas trop, il faut que je regarde dans l'aide. Je regarderai demain et je te dis si j'ai trouvé quelque chose.

Bonne soirée.

;o)
0
bestkeeper Messages postés 89 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 16 juillet 2020
13 oct. 2008 à 16:34
Merci beaucoup
0
Polux31 Messages postés 6917 Date d'inscription mardi 25 septembre 2007 Statut Membre Dernière intervention 1 novembre 2016 1 204
14 oct. 2008 à 08:44
Bonjour,

Voilà comment copier les valeurs et non les formules :
Private Sub lanceRecap()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lig1 As Long
Dim lig2 As Long
Dim i As Long
Dim str As Variant

Set ws1 = Worksheets("commandes")
Set ws2 = Worksheets("recap")
lig2 = 2

Call TriTab(True) 'True tri croissant, False tri décroissant

    For i = 1 To ind
    lig1 = 2
    str = myTab(i)
    While ws1.Range("A" & lig1).Value <> ""
        If ws1.Range("A" & lig1).Value = str Then
            ws1.Select
            ws1.Range("B" & lig1).Copy
            ws2.Select
            ws2.Range("A" & lig2).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            ws1.Select
            ws1.Range("A" & lig1).Copy
            ws2.Select
            ws2.Range("B" & lig2).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            ws1.Select
            ws1.Range("E" & lig1).Copy
            ws2.Select
            ws2.Range("C" & lig2).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            lig2 = lig2 + 1
        End If
    lig1 = lig1 + 1
    Wend
    Next i

Set ws1 = Nothing
Set ws2 = Nothing

End Sub


;o)
0
bestkeeper Messages postés 89 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 16 juillet 2020
14 oct. 2008 à 10:06
Bonjour,

même soucis sur le Private Sub Prepare tri
Erreur sur la ligne if DoesExist
0
Polux31 Messages postés 6917 Date d'inscription mardi 25 septembre 2007 Statut Membre Dernière intervention 1 novembre 2016 1 204
14 oct. 2008 à 10:27
C'est erreur de ne vient pas de ce code ... la fonction doesExist() fonctionnée avant et celle-ci n'a rien à voir avec. Le problème est ailleur.

;o)
0
bestkeeper Messages postés 89 Date d'inscription jeudi 19 avril 2007 Statut Membre Dernière intervention 16 juillet 2020
14 oct. 2008 à 10:34
Le message est le suivant :

Erreur de compilation Sub ou fonction non définie.
Et il signale la ligne indiquée ci-dessus.
0