XL 2010 Plusieurs Private Sub Worksheet_Change sur même feuille Le sujet est résolu

Mehdi8822 b

XLDnaute Nouveau
Bonjour,
comment cree les duex code de "Private Sub Worksheet_Change "sur même feuille
Voici ci-dessous un code VB qui me permet :affiche la date si un cellie non vide de 5a24: (si D<>""donc C= affiche la date )et( si L<>""donc K= affiche la date) sont mise a jours:
le code (1) est fonctionne correctement
le code (2) n'est pas fonctionne
code(1):
Private Sub Worksheet_Change(ByVal Target As Range)

Dim h, iSct As Range
Set iSct = Intersect(Target, Range("C5:C24"))
If iSct Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each h In iSct.Cells
If IsEmpty(h) Then
h.Offset(0, 1) = ""
Else
h.Offset(0, 1) = Format(Now, "mm/dd/yy")
End If
Next
Application.EnableEvents = True

End Sub

code(2):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim h, j, iSct As Range

Set iSct = Intersect(Target, Range("C5:C24"))
Set iSct = Intersect(Target, Range("K5:K24"))
If iSct Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each h In iSct.Cells
If IsEmpty(h) Then
h.Offset(0, 1) = ""
Else
h.Offset(0, 1) = Format(Now, "mm/dd/yy")
End If
If iSct Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each j In iSct.Cells
If IsEmpty(j) Then
j.Offset(0, 1) = ""
Else
j.Offset(0, 1) = Format(Now, "mm/dd/yy")
End If
Next
Application.EnableEvents = True

End Sub

Par exemmple taper une valeur en C5 pour D=18/08/2019.

Comment puis-je procéder s'il vous plait ?

Merci par avance.
 

Pièces jointes

  • test.xlsm
    13 KB · Affichages: 4

Hasco

XLDnaute Barbatruc
Repose en paix
Bonjour,

Votre macro remodelée:
VB:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim h As Range, iSct As Range

    Set iSct = Intersect(Target, Range("C5:C24,K5:K24"))
    If iSct Is Nothing Then Exit Sub
    Application.EnableEvents = False
    For Each h In iSct.Cells
         h.Offset(0, 1) = IIf(IsEmpty(h), "", Format(Now, "mm/dd/yy"))
    Next
    Application.EnableEvents = True
End Sub

P.S. : plutôt que Format(Now,"mm/dd/yy") je mettrais simplement : Date (date du jour) et le format par les outils traditionnels de formatage.

P.S. : Pourquoi mettre que le sujet est résolu dans le titre, s'il ne l'était pas?

Cordialement
 

Pièces jointes

  • test.xlsm
    15.3 KB · Affichages: 7

Jacky67

XLDnaute Barbatruc
Bonjour,
comment cree les duex code de "Private Sub Worksheet_Change "sur même feuille
Voici ci-dessous un code VB qui me permet :affiche la date si un cellie non vide de 5a24: (si D<>""donc C= affiche la date )et( si L<>""donc K= affiche la date) sont mise a jours:
le code (1) est fonctionne correctement
le code (2) n'est pas fonctionne
code(1):
Private Sub Worksheet_Change(ByVal Target As Range)

Dim h, iSct As Range
Set iSct = Intersect(Target, Range("C5:C24"))
If iSct Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each h In iSct.Cells
If IsEmpty(h) Then
h.Offset(0, 1) = ""
Else
h.Offset(0, 1) = Format(Now, "mm/dd/yy")
End If
Next
Application.EnableEvents = True

End Sub

code(2):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim h, j, iSct As Range

Set iSct = Intersect(Target, Range("C5:C24"))
Set iSct = Intersect(Target, Range("K5:K24"))
If iSct Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each h In iSct.Cells
If IsEmpty(h) Then
h.Offset(0, 1) = ""
Else
h.Offset(0, 1) = Format(Now, "mm/dd/yy")
End If
If iSct Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each j In iSct.Cells
If IsEmpty(j) Then
j.Offset(0, 1) = ""
Else
j.Offset(0, 1) = Format(Now, "mm/dd/yy")
End If
Next
Application.EnableEvents = True

End Sub

Par exemmple taper une valeur en C5 pour D=18/08/2019.

Comment puis-je procéder s'il vous plait ?

Merci par avance.
Bonjour à tous
Perso. sur cet exemple(Worksheet_Change), je ne vois pas l'intérêt d'une boucle
VB:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim iSct As Range
    Set iSct = Intersect(Target, Range("C5:C24, K5:K24"))
    If iSct Is Nothing Or Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
    If IsEmpty(iSct) Then
        iSct.Offset(0, 1) = ""
    Else
        iSct.Offset(0, 1) = Format(Now, "mm/dd/yy")
    End If
    Application.EnableEvents = True
End Sub
 

Discussions similaires

Statistiques des forums

Discussions
311 725
Messages
2 081 941
Membres
101 848
dernier inscrit
Djigbenou