Problème sur "thisworkbook"

  • Initiateur de la discussion Initiateur de la discussion xav758
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

xav758

XLDnaute Nouveau
Bonjour à tous!

Comme dit dans le titre, j'ai une macro que je veux exécuter au démarrage de mon fichier Excel, mais elle me pose de gros soucis depuis quelques heures...
Lorsque j'ouvre mon fichier, il m'affiche une erreur '57121', définie par l'application ou par l'objet.
J'ai essayé de mettre cette fonction dans un sub auto_open, mais rien à faire, ça ne marche pas du tout.
Si quelqu'un a une idée, voici le code de la macro en question :

Code:
Private Sub Workbook_Open()

Dim endline As String
Dim endline2 As String
Dim cell As Range
Dim cel As Range

endline = Worksheets("moldref").Range("D65536").End(xlUp).Row
endline2 = Worksheets("moldref").Range("A65536").End(xlUp).Row


    Set pl = Range(Worksheets("moldref").Cells(6, 4), Worksheets("moldref").Cells(endline, 4))
    For Each cell In pl
        If cell.Font.ColorIndex = 3 Then
            'Prendstoicamold.labelmold.Caption = "The mold under ref. " & cell.Value & " must be change"
           ' Prendstoicamold.labermold2.Caption = "Less than " & cell.Offset(0, 8).Value & "pcs"
            'Prendstoicamold.Show
        End If
    Next cell
    
    Set pl2 = Range(Worksheets("machineref").Cells(6, 4), Worksheets("machineref").Cells(endline, 4))
    For Each cel In pl2
        If cel.Font.ColorIndex = 3 Then
           ' Prendstoicamachine.Show
        End If
    Next cel

End Sub


Merci d'avance pour l'aide que vous pourrez m'apporter!

Xav
 
Re : Problème sur "thisworkbook"

Bonjour Creepy,

Vous trouverez le fichier Excel complet < ici >

Enfaite, la macro située dans le "thisworkbook" va chercher dans la feuille "moldref" et "machineref" les références (du moule et/ou de la machine) écrites en rouge. Si c'est le cas, alors le userform "prendstoicamold" s'affichera.
Ce qui est embêtant, c'est que si l'on exécute la macro lorsque le fichier est déjà ouvert, il n'y a pas de problème...
Ca devient vraiment problématique alors que ce fichier est presque fini! 🙁
 
Re : Problème sur "thisworkbook"

Bonjour Xav, Creepy

modifies peut être ton code comme suit :

Code:
Private Sub Workbook_Open()
Dim pl As Range, pl2 As Range
Dim endline As String, endline2 As String
Dim cell As Range, cel As Range
endline = Worksheets("moldref").Range("D65536").End(xlUp).Row
endline2 = Worksheets("moldref").Range("A65536").End(xlUp).Row
With Worksheets("moldref")
    Set pl = .Range(.Cells(6, 4), .Cells(endline, 4))
    For Each cell In pl
        If cell.Font.ColorIndex = 3 Then
            'Prendstoicamold.labelmold.Caption = "The mold under ref. " & cell.Value & " must be change"
           ' Prendstoicamold.labermold2.Caption = "Less than " & cell.Offset(0, 8).Value & "pcs"
            'Prendstoicamold.Show
        End If
    Next cell
End With
With Worksheets("machineref")
    Set pl2 = .Range(.Cells(6, 4), .Cells(endline, 4))
    For Each cel In pl2
        If cel.Font.ColorIndex = 3 Then
           ' Prendstoicamachine.Show
        End If
    Next cel
End With
End Sub

bonne journée
@+
 
Re : Problème sur "thisworkbook"

Bonjour Pierrot93

Merci pour ton code, mais il ne marche toujours pas.
Enfaite, il se bloque à la ligne
endline = Worksheets("moldref").Range("D65536").End(xlUp).Row
Mais si je rajoute une ligne de commande avant, il se bloque à celle-ci (à part si cette ligne est du style : worksheets("moldref").[A1].value = "...."
Ca me semble vraiment étrange....
 
Re : Problème sur "thisworkbook"

bonjour à tous,

en effet, c'est bizarre.... j'ai pas réussi à identifier la cause.
il plante sur :

endline = Worksheets("moldref").Range("D65536").End(xlUp).Row

visiblement, il identifie bien la feuille et la range, c'est la fin de l'instruction qu'il ne gère pas bien.

cependant, si plus tard tu lui demande de recalculer la même chose, il n'y a pas de soucis, il te renvoie bien la dernière ligne occupée...

ça sent l'objet mal géré par VBA qui pourrit tout le classeur, mais pas possible de l'identifier.
 
Re : Problème sur "thisworkbook"

Et donc est-ce qu'il est possible de faire autrement, ou l'erreur est irrémédiable?
Il s'agit d'un fichier pour un stage en entreprise, et c'est vrai que je tiens à tout bien faire...
Peut être passer cette fonction en auto_open, mais je n'y suis pas arrivé...
 
Re : Problème sur "thisworkbook"

Re

essaye comme suit :

Code:
Private Sub Workbook_Open()
Dim pl As Range, pl2 As Range
Dim endline As Integer, endline2 As Integer
Dim cell As Range, cel As Range

endline = Worksheets("Moldref").Range("D65536").End(xlUp).Row
endline2 = Worksheets("Moldref").Range("A65536").End(xlUp).Row
With Worksheets("Moldref")
    Set pl = .Range(.Cells(6, 4), .Cells(endline, 4))
    For Each cell In pl
        If cell.Font.ColorIndex = 3 Then
            'Prendstoicamold.labelmold.Caption = "The mold under ref. " & cell.Value & " must be change"
           ' Prendstoicamold.labermold2.Caption = "Less than " & cell.Offset(0, 8).Value & "pcs"
            'Prendstoicamold.Show
        End If
    Next cell
End With
With Worksheets("Machineref")
    Set pl2 = .Range(.Cells(6, 4), .Cells(endline, 4))
    For Each cel In pl2
        If cel.Font.ColorIndex = 3 Then
           ' Prendstoicamachine.Show
        End If
    Next cel
End With
End Sub

tu as des majuscules dans le nom de tes onglets donc les reporter dans le code.

Tes variables "endline" et "endline2" doivent contenir un numéro de ligne donc pas du texte, à déclarer en "integer" plutôt qu'en "string".

@+

Edition : enlevé un "stop" que j'avais positionné pour exécution pas à pas.
 
Dernière édition:
Re : Problème sur "thisworkbook"

Bonjour a tous,

Voila, je suis arrivé à faire fonctionner tout ça, en changeant un peu la syntaxe.
D'ailleur, merci encore Pierrot pour ton aide!
Par contre, je rencontre encore un problème, qui en soit ne doit pas être compliqué, mais je bloque dessus.
Enfait, la 2ème fonction 'verifmachine' de "thisworkbook" prend en compte la feuille "moldref" et non "machineref"! Ce qui fait que le userform "prendstoicamachine" affiche la ref du moule.
Je vous remet le fichier en question, et sinon, voici le code du "thisworkbook" :

Code:
Private Sub workbook_open()

verifmold
verifmachine

End Sub


Public Sub verifmold()


Dim pl As Range, pl2 As Range
Dim endline As Integer, endline2 As Integer
Dim cell As Range, celval As Range


With Worksheets("Moldref")
    Range("D65536").Select
    ActiveCell.End(xlUp).Select
    endline = ActiveCell.Row
    cell1 = Cells(6, 4).Address
    cell2 = Cells(endline, 4).Address
    Set pl = Range(cell1, cell2)
    For Each cell In pl
        If cell.Font.ColorIndex = 3 And Not cell.Value = "" Then
            Prendstoicamold.labelmold.Caption = "The mold under ref. " & cell.Value & " must be change"
            Prendstoicamold.labermold2.Caption = "Less than " & cell.Offset(0, 8).Value & "pcs may currently be maide"
            areyousure2.Label1.Caption = "Are you sure the mold, under the ref " & cell.Value & " have been removed ?"
            Prendstoicamold.Show
            
        End If
    Next cell
End With
End Sub

Public Sub verifmachine()

Dim pl As Range, pl2 As Range
Dim endline As Integer, endline2 As Integer
Dim cell As Range, celval As Range


With Worksheets("Machineref")
    Range("D65536").Select
    ActiveCell.End(xlUp).Select
    endline2 = ActiveCell.Row
    cell3 = Cells(endline2, 4).Address
    cell1 = Cells(6, 4).Address
    Set pl2 = Range(cell1, cell3)
    For Each celval In pl2
        If celval.Font.ColorIndex = 3 And Not celval.Value = "" Then
            Prendstoicamachine.labelmachine.Caption = "The " & celval.Value & "'s machine must be check!"
            Prendstoicamachine.labelmachine2.Caption = "Less than " & celval.Offset(1, 1).Value & " pcs may curently be maide"
            areyousure2.Label1.Caption = "Are you sure the machine's screw, under the ref " & celval.Value & " have been changed ?"
            Prendstoicamachine.Show
            
        End If
    Next celval
End With
End Sub
 
Re : Problème sur "thisworkbook"

Je précise!
Enfaite, il ne me charge pas ma feuille. Il faut que le fichier s'ouvre sur la feuille "moldref" pour que ça fonctionne. Donc est-ce que quelqu'un pourrait me dire comment activer la feuille? avec l'instruction worksheets("machineref").activate, il me fait une erreur...
 
Re : Problème sur "thisworkbook"

Bonjour Xav,

essaye en modifiant comme suit :

Code:
Public Sub verifmachine()
Dim pl As Range, pl2 As Range
Dim endline As Integer, endline2 As Integer
Dim cell As Range, celval As Range
With Worksheets("Machineref")
    endline2 = [COLOR="red"][B].[/B][/COLOR]Range("D65536").End(xlUp).Row
    cell3 = [COLOR="red"][B].[/B][/COLOR]Cells(endline2, 4).Address
    cell1 = [B][COLOR="Red"].[/COLOR][/B]Cells(6, 4).Address
    Set pl2 = [COLOR="red"][B].[/B][/COLOR]Range(cell1, cell3)
    For Each celval In pl2
        If celval.Font.ColorIndex = 3 And Not celval.Value = "" Then
            Prendstoicamachine.labelmachine.Caption = "The " & celval.Value & "'s machine must be check!"
            Prendstoicamachine.labelmachine2.Caption = "Less than " & celval.Offset(1, 1).Value & " pcs may curently be maide"
            areyousure2.Label1.Caption = "Are you sure the machine's screw, under the ref " & celval.Value & " have been changed ?"
            Prendstoicamachine.Show
        End If
    Next celval
End With
End Sub

Quand tu utilises un bloc "with", les diiférentes instructions concernées par ce bloc, doivent être précédées d'un point.

D'autre part, l'instruction :

Code:
worksheets("Machineref").activate

devrait fonctionner, si tu mets la majuscule au nom de la feuille.

bonne journée
@+
 
Re : Problème sur "thisworkbook"

Non, rien à faire, la feuille ne veut pas s'activer!
J'ai essayé ta méthode Pierrot, mais rien à faire, il ne me reconnait pas les fonctions...
Si tu as une autre option pour ouvrir une feuille au démarrage, je suis preneur!
 
Re : Problème sur "thisworkbook"

Re

oui effectivement dans ton classeur cela bogue... Mais la syntaxe fonctionne bien éxécutée à partir d'un module placé dans un autre classeur.

Mais beaucoup de modules, avec des procédures dont le nom est un mot réservé vba, comme beaucoup d'usf et dont certains objets ont des noms équivoques... Pas sur que cela vienne de là, mais à mon avis, il serait bon de faire un peu de "ménage" dans ton projet.

bon après midi
@+

Edition : n'oublie pas de nétoyer également les procédures dans les modules des différentes feuilles, les noms des boutons ayant été modifiés, mais les procédures d'origine sont restées...
style, "Private Sub CommandButton1_Click()"
 
Dernière édition:
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

  • Question Question
Microsoft 365 worksheet_change
Réponses
29
Affichages
532
Réponses
3
Affichages
673
Réponses
0
Affichages
471
Réponses
4
Affichages
426
Retour