UseForm : Utiliser un CommandButton créé Dynamiquement
Bonjour,
J’ai créer un UserForm (UserForm1)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| Private Sub CommandButton1_Click()
Dim a As Integer
nb = TextBox1.Value
Dim dist As Integer
dist = 20
Dim Compt As Integer
For Compt = 1 To nb
With UserForm2.Controls.Add("Forms.textbox.1", "Textbox" & Compt, True)
.Top = dist
.Left = 10
.Width = 100
.Height = 15
UserForm2.Width = .Width + .Left * 2.5
End With
dist = dist + 15
Next
UserForm2.Height = dist + 100
UserForm2.Show
End Sub
Private Sub UserForm_AddControl(ByVal Control As MSForms.Control)
MsgBox TypeName(Control) & ": " & Control.Name
End Sub |
Qui me permet de lancer un autre UserForm (UserForm2)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| Private Sub UserForm_Activate()
With UserForm2.Controls.Add("Forms.Label.1", "Label1", True)
.Caption = "Entrer les mots :"
.Top = 5
.Left = 10
.Width = 100
.Height = 15
End With
With UserForm2.Controls.Add("Forms.Label.1", "Label2", True)
.Caption = "Feuille où copier :"
.Top = UserForm2.Height - 90
.Left = 10
.Width = 100
.Height = 15
End With
With UserForm2.Controls.Add("Forms.textbox.1", "TextboxFeuille", True)
.Top = UserForm2.Height - 75
.Left = 10
.Width = 100
.Height = 15
End With
Dim obj As Control
Set obj = UserForm2.Controls.Add("Forms.CommandButton.1", "CommandButton1", True)
With obj
.Top = UserForm2.Height - 50
.Width = 50
.Left = (UserForm2.Width / 2) - (.Width / 2)
.Height = 25
.Caption = "Chercher"
End With
MsgBox UserForm2.Controls("TextboxFeuille").Name
With UserForm2.CodeModule
j = .CountOfLines
.InsertLines j + 1, "Sub " & CommandButton1 & "_Click()"
.InsertLines j + 2, "call (Button_chercher)"
.InsertLines j + 3, "End Sub"
End With
End Sub
Private Sub UserForm_AddControl(ByVal Control As MSForms.Control)
MsgBox TypeName(Control) & ": " & Control.Name
End Sub
Private Sub Button_chercher()
Dim i As Integer
Dim j As Integer
Dim k As Integer
For i = UserForm1.TextBox2 To UserForm1.TextBox3
For j = UserForm1.TextBox4 To UserForm1.TextBox5
For k = 1 To UserForm1.TextBox1
If Cells(i, j) = UserForm2.Controls("Textbox" & k).Caption Then
Cells(i, j).EntireRow.Select
Selection.Copy
Sheets(UserForm2.Controls("TextboxFeuille").Caption).Activate
Cells(cpt + 1, 1).Activate
ActiveSheet.Paste
cpt = cpt + 1
End If
Next
Next
Next
End Sub |
Et mon problème est que je n’arrive pas à utiliser la macro évènementielle du CommandButton1, j’ai essayé plusieurs solutions en cherchant sur internet mais pas moyen...
Ce bouton permet de lancer la recherche dans la feuille excel des cellules contenant les mots indiqué dans les textbox créer dans le userform2 par le userform1.
Merci pour vos futures réponses,
Amicalement