Comment associer 2 codes VBA

Bonjour

J'ai 2 codes vba que je souhaiterais mettre sur chaque mois de mon exemple.

* Le 1er qui n'autorise pas plus de 12 CA par jour.

"Public Flag As Boolean

Dim nCA&, nCAJ&, nCAN&, n12h&

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect([C9:M46], Target) Is Nothing Then Exit Sub

nCA = Application.CountIf(Intersect([9:46], Target.EntireColumn), "CA")

nRPM = Application.CountIf(Intersect([9:46], Target.EntireColumn), "RPM")

nCAJ = Application.CountIf(Intersect([9:46], Target.EntireColumn), "CAJ*")

nCAN = Application.CountIf(Intersect([9:46], Target.EntireColumn), "*CAN")

If (nCA + nCAJ + nRPM) > 12 Or (nCA + nCAN + nRPM) > 12 Then

MsgBox "Le nombre maximal de 12 CA total est déjà atteint !", vbCritical, "Saisie CA"

Target.Interior.Color = RGB(255, 255, 255)

Application.EnableEvents = False

Target.ClearContents

Application.EnableEvents = True

End If

End Sub"

* Et le 2ème qui n'autorise pas plus de 5 12h par jour

"Public Flag As Boolean

Dim n12h&

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect([C9:M46], Target) Is Nothing Then Exit Sub

n12h = Application.CountIf(Intersect([9:46], Target.EntireColumn), "12h")

If n12h > 5 Then

MsgBox "Le nombre maximal de 12h pour ce jour est déjà atteint !", vbCritical, "Saisie 12h"

Target.Interior.Color = RGB(255, 255, 255)

Application.EnableEvents = False

Target.ClearContents

Application.EnableEvents = True

End If

End Sub"

Dans mon exemple je ne l'ai essayé que sur le mois de décembre mais je ne sais pas comment les associer.

Un code seul sur la page fonctionne mais comment dois-je m'y prendre pour associer les 2 codes afin qu'ils fonctionnent comme ci-dessus?

Merci par avance

6essai.zip (77.42 Ko)

Bonjour,

à tester,

Public Flag As Boolean
Dim nCA&, nCAJ&, nCAN&, n12h&

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect([C9:M46], Target) Is Nothing Then Exit Sub

nCA = Application.CountIf(Intersect([9:46], Target.EntireColumn), "CA")
nRPM = Application.CountIf(Intersect([9:46], Target.EntireColumn), "RPM")
nCAJ = Application.CountIf(Intersect([9:46], Target.EntireColumn), "CAJ*")
nCAN = Application.CountIf(Intersect([9:46], Target.EntireColumn), "*CAN")
n12h = Application.CountIf(Intersect([9:46], Target.EntireColumn), "12h")

If (nCA + nCAJ + nRPM) > 12 Or (nCA + nCAN + nRPM) > 12 Or n12h > 5 Then
    MsgBox "Le nombre maximal de 12 CA total est déjà atteint !", vbCritical, "Saisie CA"
    Target.Interior.Color = RGB(255, 255, 255)
    Application.EnableEvents = False
    Target.ClearContents
    Application.EnableEvents = True
End If

End Sub
Rechercher des sujets similaires à "comment associer codes vba"