Activation d'un commanbutton via un module classique et non dans la feuille
Bonjour à tous,
J'essaye de trouver un moyen de lancer une macro via un commandbutton qui a été créé via un module classique.
En général cela est assez simple: on double clique sur la bouton ce qui crée un module qui est créé automatiquement dans la feuille excel et qu'il est facile d'éditer.
Ma question: comment faire pour rendre cette procédure automatique càd directement codé dans un module classique (qui ne se situe pas dans la feuille excel) ?
Est il facile d'additionner un module dans la feuille via un module classique? Autre possibilité, est il possible de coder cela dans des modules classiques? J'y arrive très bien par exemple avec un scrollbar via la commande .onaction. Existe t'il un moyen similaire pour un commandbutton? OnClick?
Le code actuel qui me sert à insérer le commandbutton:
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=320, Top:=14, Width:=100, Height:=30).Name = "Pause"
Un grand merci d'avance pour vos éclairages et bonne journée!
Bonsoir
Un exemple de ton fichier serait le bienvenu
Cordialement
Le code complet du module :
Seule la toute fin est a priori utile (la partie de mon précédent post) pour ma question. Ce qu'il y a avant permet de créer un graphique que je désire rendre dynamique en cliquant sur le fameux commandbutton que je fais apparaître en même temps que le graphique.
Dim ScanPoints As Integer
Dim axeX, axeY, titre As Range 'pour construction des graphes
Dim nbColTranspose As Integer
Sub Animated_Graph_f_Temperature()
'Création du nouvel onglet pour les animations'
Sheets.Add(after:=Worksheets(4)).Name = "Movie_Temperature"
Set sh5 = Sheets("Movie_Temperature")
'calcul le nombre de points (différentes Températures) = nb de graphes à scanner
'utilisé par l'offset ultérieur :
ScanPoints = Worksheets(1).Range("CircularDichroism").Columns.Count - 1 '-1 car 1ère ligne n'est pas une température
Set sh3 = Sheets("Données_Transposées")
Set sh4 = Sheets("Graphs_exp=f(lambda)")
If CheckExists("Animated_graph") = False Then
'nbColTranspose = nb de lambda dans le graphe = nb de points axe des X
nbColTranspose = sh3.Range("Transpose_CD").Columns.Count
Dim Y_min, Y_mas As Double 'limite pour l'axe des Y pour que graphique ne change pas d'échelle lors du scan
Dim plage As Range 'plage pour scan Ymax et Y min
sh3.Select
Range("Transpose_CD").Select
Selection.Offset(1, 1).Select 'offset permet de supprimer 1ère ligne / colonne
Set plage = Selection
Y_min = Int(WorksheetFunction.min(plage))
Y_max = Int(WorksheetFunction.max(plage))
'No ligne de Tmin et Tmax pour les colorier correctement en fonction de leur position
Dim Tmax_Row, Tmin_Row As Integer
Dim Tmax, Tmin As Double
Range("Transpose_CD").Columns(1).Select
Tmax = WorksheetFunction.max(Selection)
Tmin = WorksheetFunction.min(Selection)
Tmax_Row = Selection.Find(Tmax, lookat:=xlWhole).Row
Tmin_Row = Selection.Find(Tmin, lookat:=xlWhole).Row
'lambda min et max pour bornes axe X
Dim lambdamax, lambdamin As Double
Range("Transpose_CD").Rows(1).Select
lambdamax = WorksheetFunction.max(Selection)
lambdamin = WorksheetFunction.min(Selection)
sh5.Select
Dim ChartObjects As ChartObject
ActiveSheet.ChartObjects.Add(100, 70, 400, 500).Name = "Animated_graph"
'définition des axes X et Y du premier graphe
Set axeY = sh3.Range("$B$3").Resize(, nbColTranspose - 2)
Set axeX = sh3.Range("$B$2").Resize(, nbColTranspose - 2)
Set titre = sh3.Range("$A$3")
With ActiveSheet.Shapes("Animated_graph").Chart
.HasLegend = False
.ChartType = xlXYScatterSmoothNoMarkers
.SeriesCollection.NewSeries
With .SeriesCollection(1)
.Values = axeY
.XValues = axeX
.Name = titre.Value
End With
If Tmax_Row < Tmin_Row Then
.ChartTitle.Text = "Decreasing T° ramp: " & Tmax & " -> " & Tmin & " °C"
Else:
.ChartTitle.Text = "Increasing T° ramp: " & Tmin & " -> " & Tmax & " °C"
End If
With .ChartTitle.Font
.Size = 15
.Bold = True
End With
'Définition des axes X et Y'
.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
.Axes(xlValue, xlPrimary).HasTitle = True 'nom de l'axe des Y
.Axes(xlValue, xlPrimary).AxisTitle.Text = "CD (mdeg)"
.Axes(xlValue, xlPrimary).AxisTitle.Font.Size = 13
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = ChrW(955) & " (nm)" 'nom de l'axe des X"
.Axes(xlCategory, xlPrimary).AxisTitle.Font.Size = 13
'limite en CD de l'axe Y
With .Axes(xlValue)
.MinimumScale = Y_min - 2
.MaximumScale = Y_max + 2
.MajorUnit = 10
.MinorUnit = 2.5
End With
With .Axes(xlCategory)
.MinimumScale = lambdamin - 2
.MaximumScale = lambdamax + 2
.MajorUnit = 10
.MinorUnit = 5
End With
'Apparence des axes X et Y'
With .Axes(xlCategory)
.MajorTickMark = xlOutside
.MinorTickMark = xlInside
.Format.Line.Weight = 1
.TickLabels.Font.Color = msoThemeColorText1
.TickLabels.Font.Bold = True
.TickLabels.Font.Size = 11
End With
With .Axes(xlValue)
.MajorTickMark = xlOutside
.MinorTickMark = xlInside
.Format.Line.Weight = 1
.TickLabels.Font.Color = msoThemeColorText1
.TickLabels.Font.Bold = True
.TickLabels.Font.Size = 11
End With
End With
'insertion textbox T°C pour actualisation de T°
ActiveSheet.ChartObjects("Animated_graph").Activate
ActiveChart.Shapes.AddTextbox(msoTextOrientationHorizontal, 327, 54, 60, 25).Name = "TxtBox-Temp"
ActiveChart.Shapes("TxtBox-Temp").TextFrame2.TextRange.Characters.Text = titre.Value & "°C"
With ActiveChart.Shapes("TxtBox-Temp").TextFrame2.TextRange.Characters.Font
.Bold = True
If Tmax_Row < Tmin_Row Then
.Fill.ForeColor.RGB = RGB(255, 25, 25) 'rouge
Else: .Fill.ForeColor.RGB = RGB(25, 25, 255) 'bleu
End If
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Size = 14
End With
ActiveChart.Shapes.Range(Array("TxtBox-Temp")).Select
With Selection.ShapeRange.Fill
.ForeColor.RGB = RGB(255, 0, 0)
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.Transparency = 0.25
End With
With Selection.ShapeRange.Line
.ForeColor.ObjectThemeColor = msoThemeColorText1
.Weight = 1
End With
' insert 2ème textbox pour text température lors animation
ActiveChart.Shapes.AddTextbox(msoTextOrientationHorizontal, 327, 90, 60, 25).Name = "TxtBox-Temp2"
ActiveChart.Shapes("TxtBox-Temp2").TextFrame2.TextRange.Characters.Text = ""
With ActiveChart.Shapes("TxtBox-Temp2").TextFrame2.TextRange.Characters.Font
.Bold = True
.Fill.ForeColor.RGB = RGB(128, 128, 128)
.Size = 14
End With
ActiveChart.Shapes.Range(Array("TxtBox-Temp2")).Select
With Selection.ShapeRange.Fill
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.Transparency = 0.25
End With
With Selection.ShapeRange.Line
.ForeColor.ObjectThemeColor = msoThemeColorText1
.Weight = 1
End With
ActiveChart.Shapes("TxtBox-Temp2").Visible = False
'Aspect graphe
ActiveSheet.ChartObjects("Animated_graph").Activate
With ActiveChart
.SetElement (msoElementPrimaryCategoryGridLinesMinorMajor)
.SetElement (msoElementPrimaryValueGridLinesMajor)
.FullSeriesCollection(1).Select
With Selection.Format.Line
If Tmax_Row < Tmin_Row Then
.ForeColor.RGB = RGB(255, 25, 25) 'rouge
Else: .ForeColor.RGB = RGB(25, 25, 255) 'bleu
End If
End With
End With
With ActiveSheet.Shapes("Animated_graph")
With .Line
.ForeColor.ObjectThemeColor = msoThemeColorAccent3
.Weight = 1.25
End With
With .Fill
.ForeColor.RGB = RGB(25, 25, 255) 'bleu
.ForeColor.Brightness = 0.9
End With
End With
With ActiveChart.ChartTitle
With .Format.Fill
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
End With
With ActiveChart.Axes(xlCategory).MinorGridlines.Format.Line
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.Brightness = 0.6
.Weight = 0.6
End With
With ActiveChart.Axes(xlCategory).MajorGridlines.Format.Line
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.Brightness = 0.3
.Weight = 0.6
End With
With ActiveChart.Axes(xlValue).MajorGridlines.Format.Line
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.Brightness = 0.4
.Weight = 0.6
End With
End With
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=160, Top:=14, Width:=100, Height:=30).Name = "Animation"
With ActiveSheet.OLEObjects("Animation").Object
.Caption = "Play Animation"
.ForeColor = vbMagenta
.FontBold = True
.BackColor = vbYellow
End With
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=320, Top:=14, Width:=100, Height:=30).Name = "Pause"
With ActiveSheet.OLEObjects("Pause").Object
.Caption = "Pause"
.ForeColor = vbMagenta
.FontBold = True
.BackColor = vbYellow
End With
Else: Call Animation_Temperature
End If
End Sub
- Messages
- 4'100
- Excel
- 2021 FR 64 bits
- Inscrit
- 13/06/2016
- Emploi
- bénévole associations Goutte d'Or
Bonjour,
Il vous suffit d'utiliser dans le code associé à la feuille où vous avez créé vos contrôles ActiveX, les procédures événementielles :
Private Sub Animation_Click()
End Sub
Private Sub Pause_Click()
End Sub
Bonjour Thev,
Merci pour la réponse. La feuille étant créée par un autre module, il me faut une manière d'y intégrer les procédures événementielles à partir du même module classique qui a servi à faire la feuille. Autrement, l'utilisateur lambda devra ajouter ces procédures à la main après utilisation de la macro originelle ce qui n'est pas très "user friendly".
Une autre possibilité serait d'intégrer ces procédures dans le module classique original avec un appel de procédure similaire à onAction utilisable avec un scrollbar. Je n'ai malheureusement pas trouvé l'équivalent de onAction pour un CommandButton malheureusement.
Bonne journée
- Messages
- 4'100
- Excel
- 2021 FR 64 bits
- Inscrit
- 13/06/2016
- Emploi
- bénévole associations Goutte d'Or
Bonjour,
Je ne comprends pas pourquoi vous vous compliquez la vie avec un contrôle ActiveX. Une simple forme (un rectangle par exemple) suffit :
Set bouton = ActiveSheet.Shapes.AddShape(Type:=msoShapeRectangle, Left:=160, Top:=14, Width:=100, Height:=30)
With bouton
.Name = "Animation"
With .TextFrame.Characters
.Text = "Play Animation"
.Font.Bold = True
End With
.Fill.ForeColor.RGB = RGB(255, 0, 255) 'magenta
.Fill.BackColor.RGB = rgbYellow
.OnAction = ""
End With
Je vais tester ça ce soir! Merci énormément pour la suggestion qui a l'énorme mérite d'être très simple si le onaction fonctionne avec une simple forme !
Je suis en fait débutant autodidacte en vba et très loin de maîtriser toutes les possibilités du langage. Respect à vous les aidants du forum !
Je reviendrai vers vous avec le résultat.
Encore merci
Ça marche parfaitement, merci thev! Je clos le sujet.