Bonjour a tous,
je poste un nouveau message toujours concernant mon probleme de macro excel au comportement etrange.
alors j'ai modifié mon code depuis et simplifier mon application et je pense avoir identifier le probleme.
voila je demande via une Msg box a l'utilisatuer de mon fichier excel si il veut creer des boutons graphiques pour chaque feuille du classeur. j'ai donc une application sub dans un module qui me permet de de gerer les differentes reponse de l'utilisateur et d appeler la procedure de creation de buton. jsuque la ok ... la macro d interaction avec l utilsateur etant appeler des l'ouverture du classeur ,.
mais voila quand je ceer un bouton la procedure se deroule normalement jusqu'a la fin car s'affiche un message qui ne fait pas parti de la prcedure elle meme juste apres la creation du bouton sur la feuille. mais au monent ou je click sur ce message bime .... plantage Excel tout s'arrete , se ferme, et message : la memoire ... ne pas pas etre ecrite apparait !!!
ce qui me chiffone c'est que si je lance ma procedure de creation independament elle marche tres bien pour chaque feuille.
de plus et c'est la le plus etrange, si j'ouvre le classeur que je refuse de mettre des bouton et que je vais dans VBa editor, que la je fais debug > compiler et que je relance le programme au niveau de l'ouverture de classeur tout se deroule pour le mieux .
en revanche si je ferme le classeur et que je le reouvre et que je fais directement ce que la macro me demande la j'ai a nouveau une erreur et tout plante ...
alors se peut il que mon projet ne se compile pas a l'ouverture?
y a t il moyen de forcer la compilation a l'ouverture quelque chose du style application.debug.run
ou voyez vous une erreur dan mon code ..
merci de m'aider car la je suis pommé j'ai tout eclusé comme pirouette pour me debarraser de cela mais rien y fait et je ne trouve rien sur le net a ce propos .
voila mon code si vous pouvez le tester pour me dire ce qui vous en pensé je vous en serai tres obligé...
PROCEDURE D INTERFCE AVEC UTILISATEUR
Sub Open_Workbook()
Dim j As Integer
Dim error As Boolean
Dim obj As OLEObject
New_try:
Dim i As Long
Dim answser 'idéclaration de la variable, avec reponse comme nom
Dim answser_graphs
ActiveWorkbook.Activate
answser = MsgBox("Do you want to add Graphical Application ?", vbYesNo + vbQuestion, "Validation")
Select Case answser
Case vbYes 'on click yes
On Error GoTo Problem
For i = 1 To Worksheets.Count
Worksheets(i).Activate
Worksheets(i).Visible = True
error = False
For Each obj In Worksheets(i).OLEObjects
If obj.Name = "GraphButton" Or obj.Name = "ValidationButton" Then
error = True
Exit For
End If
Next obj
answser_graphs = MsgBox("Add Graphical Application on sheet : " & Worksheets(i).Name, vbYesNo + vbQuestion, "Sheet Insertion Button")
Select Case answser_graphs
Case vbYes 'on click yes
If error = False Then
Call Module1.Button_Graph_creation(Worksheets(i).Name) ||||ICI J APPEL MA CREATION DE BOUTON
MsgBox " Graphs Buttons added successfully !", vbInformation ||||| LA CA PASSE
|||||||| LA CA PLANTE
Else
MsgBox " Sheet : " & ActiveSheet.Name & " already contains Graphs applications ", vbInformation
End If
Case vbNo 'on click no
MsgBox " No Button added !", vbInformation
End Select 'end
Next i
Case vbNo 'on click no
End Select 'end
Exit Sub
Problem:
MsgBox "Error occured, please try again !", vbInformation
j = j + 1
If j < 3 Then
GoTo New_try
Else
MsgBox " This error need you to restar Excel if you want to add the Graphs, sorry", vbInformation
Exit Sub
End If
End Sub
PROCEDURE DE CREATION DE BOUTON DYNAMIQUE
'Purpose: Allow to create dynamically 2 Buttons on the sheet to add graph application
Sub Button_Graph_creation(Worksheet_Name As String) ' Worksheet_Name is the name of the sheet were we place the buttons
Dim Code As String 'code will contain the code of the bur´toon we will create
Dim NextLine As String ' to situate where we are placing the code
Dim Last_cell As Long ' to know where is the last cell in used
Dim oOLE_G As Object ' object type to identifie an create the button Graph
Dim oOLE_V As Object ' object type to identifie an create the button Validation
Dim Ws As Worksheet ' the worksheet where we will use the button
'Application.ScreenUpdating = False
Set Ws = Worksheets(Worksheet_Name)
Last_cell = Ws.UsedRange.Columns.Count ' to know what is the last column in used
'to add a first button
Set oOLE_G = Ws.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=Ws.Cells(1, Last_cell).Left, Top:=5, Width:=100, Height:=50)
' to change the name of this button and it captation
With oOLE_G
.Name = "GraphButton"
.Object.Caption = "Graphs"
'Ws.OLEObjects(Ws.OLEObjects.Count).Object.Caption = "Graphs"
End With
' the code we are placing in the procedure which is called after a click on this buton
Code = "Sub GraphButton_Click()" & vbCrLf
Code = Code & " Load Menu " & vbCrLf
Code = Code & "Menu.Show " & vbCrLf
Code = Code & "End Sub"
With ActiveWorkbook.VBProject.VBComponents.Item(Ws.CodeName).CodeModule
NextLine = (.CountOfLines + 1)
.insertlines NextLine, Code
End With
Set oOLE_G = Nothing ' realse the parameter
Set Ws = Nothing ' realse the parameter
'Application.ScreenUpdating = True
End Sub
PROCEDURE D APPEL AU DEMARRAGE
Sub Workbook_Open()
Call Open_Workbook
End Sub