Fusionner 2 macros identiques

Résolu/Fermé
Drimo_8885 Messages postés 19 Date d'inscription lundi 22 novembre 2021 Statut Membre Dernière intervention 19 avril 2024 - 14 déc. 2021 à 11:32
Drimo_8885 Messages postés 19 Date d'inscription lundi 22 novembre 2021 Statut Membre Dernière intervention 19 avril 2024 - 14 déc. 2021 à 16:11
Rébonjour,

J'ai crée 2 userforms qui, séparément fonctionnent bien, mais j'aimerais les intégrer sur la même feuille mais en remplissant 2 cellules différentes de ma feuille :
M11 pour NOMENCLATURE et F23 pour Codecompte :
Ci-dessous les codes dans ma feuille principale pour chaque Userform.

Sauf qu'à l'exécution ça m'affiche "erreur de débogage Nom", j'ai compris que les 2 macros ont le même nom et ne peuvent être exécuter ensemble.

Je cherche donc une solution pour compiler les 2 et les faire fonctionner ensemble.
Merci par avance pour vos réponses !

Userform1:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D11")) Is Nothing Then

If Target.Value = "Europe" Then
NOMENCLATURE.att1.Caption = 4
NOMENCLATURE.att2.Caption = 2
NOMENCLATURE.att3.Caption = 2
NOMENCLATURE.att4.Caption = 5
End If

If Target.Value = "Afrique" Then
NOMENCLATURE.att1.Caption = 7
NOMENCLATURE.att2.Caption = 2
NOMENCLATURE.att3.Caption = 2
NOMENCLATURE.att4.Caption = 2
End If

If Target.Value = "Asie" Then
NOMENCLATURE.att1.Caption = 4
NOMENCLATURE.att2.Caption = 2
NOMENCLATURE.att3.Caption = 2
NOMENCLATURE.att4.Caption = 5
End If

If Target.Value = "Amérique" Then
NOMENCLATURE.att1.Caption = 4
End If

NOMENCLATURE.code1.MaxLength = Val(NOMENCLATURE.att1.Caption)
NOMENCLATURE.code2.MaxLength = Val(NOMENCLATURE.att2.Caption)
NOMENCLATURE.code3.MaxLength = Val(NOMENCLATURE.att3.Caption)
NOMENCLATURE.code4.MaxLength = Val(NOMENCLATURE.att4.Caption)

NOMENCLATURE.code2.Visible = False
NOMENCLATURE.code3.Visible = False
NOMENCLATURE.code4.Visible = False

NOMENCLATURE.Show
End If
End Sub
___________________________________________________________________________________________________________
Userform 2 :
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D11")) Is Nothing Then

If Target.Value = "Europe" Then
Codecompte.att1.Caption = 4
Codecompte.att2.Caption = 2
Codecompte.att3.Caption = 2
Codecompte.att4.Caption = 11
Codecompte.att5.Caption = 3
End If

If Target.Value = "Afrique" Then
Codecompte.att1.Caption = 7
Codecompte.att2.Caption = 2
Codecompte.att3.Caption = 2
Codecompte.att4.Caption = 11
Codecompte.att5.Caption = 3
End If

If Target.Value = "Asie" Then
Codecompte.att1.Caption = 4
Codecompte.att2.Caption = 2
Codecompte.att3.Caption = 2
Codecompte.att4.Caption = 11
Codecompte.att5.Caption = 3
End If


Codecompte.code1.MaxLength = Val(Codecompte.att1.Caption)
Codecompte.code2.MaxLength = Val(Codecompte.att2.Caption)
Codecompte.code3.MaxLength = Val(Codecompte.att3.Caption)
Codecompte.code4.MaxLength = Val(Codecompte.att4.Caption)
Codecompte.Code5.MaxLength = Val(Codecompte.att5.Caption)

Codecompte.code2.Visible = False
Codecompte.code3.Visible = False
Codecompte.code4.Visible = False
Codecompte.Code5.Visible = False

Codecompte.Show
End If
End Sub
A voir également:

2 réponses

cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
14 déc. 2021 à 11:53
Bonjour,

comme ceci:

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Application.Intersect(Target, Range("M11")) Is Nothing Then
'code
NOMENCLATURE.Show
End If
If Not Application.Intersect(Target, Range("F23")) Is Nothing Then
'code
Codecompte.Show
End If
End Sub

0
Drimo_8885 Messages postés 19 Date d'inscription lundi 22 novembre 2021 Statut Membre Dernière intervention 19 avril 2024 1
Modifié le 14 déc. 2021 à 14:18
Bonjour cs_Le Pivert,

Edit : j'ai pu résoudre le problème

J'ai peut-être oublié de préciser que j'ai une liste déroulante dans "D11" (Europe, Asie, Afrique et Amérique) et c'est selon le choix que je fais dans cette cellule que je remplis d'abord la cellule "M11" puis la cellule "F23".

J'ai essayé votre formule mais cela ne donne rien.

Bien à vous !
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
14 déc. 2021 à 14:24
Cela change tout!

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D11")) Is Nothing Then
If Target.Value = "Europe" Then
NOMENCLATURE.att1.Caption = 4
NOMENCLATURE.att2.Caption = 2
NOMENCLATURE.att3.Caption = 2
NOMENCLATURE.att4.Caption = 5
Codecompte.att1.Caption = 4
Codecompte.att2.Caption = 2
Codecompte.att3.Caption = 2
Codecompte.att4.Caption = 11
Codecompte.att5.Caption = 3
ElseIf Target.Value = "Afrique" Then
NOMENCLATURE.att1.Caption = 7
NOMENCLATURE.att2.Caption = 2
NOMENCLATURE.att3.Caption = 2
NOMENCLATURE.att4.Caption = 2
Codecompte.att1.Caption = 7
Codecompte.att2.Caption = 2
Codecompte.att3.Caption = 2
Codecompte.att4.Caption = 11
Codecompte.att5.Caption = 3
ElseIf Target.Value = "Asie" Then
NOMENCLATURE.att1.Caption = 4
NOMENCLATURE.att2.Caption = 2
NOMENCLATURE.att3.Caption = 2
NOMENCLATURE.att4.Caption = 5
Codecompte.att1.Caption = 4
Codecompte.att2.Caption = 2
Codecompte.att3.Caption = 2
Codecompte.att4.Caption = 11
Codecompte.att5.Caption = 3
ElseIf Target.Value = "Amérique" Then
NOMENCLATURE.att1.Caption = 4
End If

NOMENCLATURE.code1.MaxLength = Val(NOMENCLATURE.att1.Caption)
NOMENCLATURE.code2.MaxLength = Val(NOMENCLATURE.att2.Caption)
NOMENCLATURE.code3.MaxLength = Val(NOMENCLATURE.att3.Caption)
NOMENCLATURE.code4.MaxLength = Val(NOMENCLATURE.att4.Caption)

NOMENCLATURE.code2.Visible = False
NOMENCLATURE.code3.Visible = False
NOMENCLATURE.code4.Visible = False


Codecompte.code1.MaxLength = Val(Codecompte.att1.Caption)
Codecompte.code2.MaxLength = Val(Codecompte.att2.Caption)
Codecompte.code3.MaxLength = Val(Codecompte.att3.Caption)
Codecompte.code4.MaxLength = Val(Codecompte.att4.Caption)
Codecompte.Code5.MaxLength = Val(Codecompte.att5.Caption)

Codecompte.code2.Visible = False
Codecompte.code3.Visible = False
Codecompte.code4.Visible = False
Codecompte.Code5.Visible = False

NOMENCLATURE.Show
Codecompte.Show
End If
End Sub


voilà
0
Drimo_8885 Messages postés 19 Date d'inscription lundi 22 novembre 2021 Statut Membre Dernière intervention 19 avril 2024 1
14 déc. 2021 à 15:09
Merci beaucoup .... ça fonctionne parfaitement !!
0
Drimo_8885 Messages postés 19 Date d'inscription lundi 22 novembre 2021 Statut Membre Dernière intervention 19 avril 2024 1
14 déc. 2021 à 15:17
Juste un petit problème, il m'affiche le userform codecompte pour "Amérique" alors que je n'en veux pas et au lieu de le fermer, j'aimerais qu'il n'apparaisse même pas.

J'ai essayé avec Hide mais sans succès.

Belle aprèm !
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728 > Drimo_8885 Messages postés 19 Date d'inscription lundi 22 novembre 2021 Statut Membre Dernière intervention 19 avril 2024
14 déc. 2021 à 15:35
remplacer:

NOMENCLATURE.Show
Codecompte.Show
End If
End Sub

par

NOMENCLATURE.Show
If Target.Value = "Amérique" Then Exit Sub
Codecompte.Show
End If
End Sub


@+
0
Drimo_8885 Messages postés 19 Date d'inscription lundi 22 novembre 2021 Statut Membre Dernière intervention 19 avril 2024 1 > cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024
14 déc. 2021 à 16:11
Merci infiniment ... tout fonctionne maintenant.

Excellente fin de journée à vous !
0