Microsoft 365 Private Sub Worksheet_Change(ByVal Target As Range)

  • Initiateur de la discussion Initiateur de la discussion pat66
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

pat66

XLDnaute Impliqué
bonjour le forum,

Au secours !!
je cherche un peu d'aide car cette macro, qui doit être très mal rédigée, bloque et ferme Excel au moindre changement

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets("simulateur").Unprotect "toto"
Application.ScreenUpdating = False
Application.EnableEvents = False

If Not Intersect(Target, Me.Range("K4")) Is Nothing Then
Range("S6") = Range("Y50")
Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
End If

If Not Intersect(Target, Me.Range("S6")) Is Nothing Then
'Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
'End If

If Not Intersect(Target, Me.Range("Y6")) Is Nothing Then
Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
End If

If Not Intersect(Target, Me.Range("F27")) Is Nothing Then
If Range("F27").Value < FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")" Then
MsgBox "Attention ! " & Chr(10) & Chr(10) & "Le résultat saisit est inférieur à " & Range("B162").Value & " €, vbInformation + vbOKOnly
Else
If Range("S6").Value <> Range("Q49").Value Then
MsgBox "Attention ! " & Chr(10) & Chr(10) & "Ce montant ne correspond pas, vbInformation + vbOKOnly
End If
End If
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
Worksheets("simulateur").protect "toto"
End Sub
 
Dernière modification par un modérateur:
Solution
VB:
Private Sub Worksheet_Change(ByVal Target As Range)
'
    Worksheets("simulateur").Unprotect "toto"
    Application.ScreenUpdating = False
    Application.EnableEvents = False

    Select Case Target.Address(0, 0)
    Case "K4"
        Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
        Range("S6") = Range("Y50")
    Case "S6"
        Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
    Case "Y6"
        Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
    End Select

    If Not Intersect(Target, Me.Range("F27")) Is Nothing Then
        If Range("F27").Value < Range("B162").Value Then
            MsgBox "1"
        Else
'...
re

bonjour
la bonne blague 😉
c'est une histoire sans fin tron truc
au change de "S" tu modifie F27
au dans le même event change tu fait une modif F7
ca tourne en rond ton truc
desactive les event pendant le code et reactive à la fin

et cela ne veux rien dire
VB:
If Range("F27").Value < FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")" Then
tu test si la valeur est plus petite que le string de la formule 🤣🤣

bref tout à refaire
 
Bonjour patricktoulon,

merci du conseil,

tout ça parce que je pensais pouvoir faire en sorte que F27 est 2 fonctions, à savoir :
- soit contienne en permanence =SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
ou en tout cas lorsque je saisis une valeur dans K4 ou S6 ou Y6

- mais que je puisse aussi y saisir directement une valeur, ce qui a pour conséquence d'effacer la formule

tant pis, je vais chercher une autre solution

cdt
 
Dernière édition:
re,

voila j'ai testé cette approche et ca a l'air de fonctionner, qu'en pensez vous ? merci

VB:
Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets("simulateur").Unprotect "toto"
Application.ScreenUpdating = False
Application.EnableEvents = False

Select Case Target.Address(0, 0)
  Case Is = "K4"
    Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
    Range("S6") = Range("Y50")
  Case Is = "S6"
    Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
  Case Is = "Y6"
   Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
End Select

If Not Intersect(Target, Me.Range("F27")) Is Nothing Then
If Range("F27").Value < Range("B162").Value Then
MsgBox "1"
Else
'If Range("S6").Value <> Range("Q49").Value Then
MsgBox "2"
End If
End If

Application.EnableEvents = True
Application.ScreenUpdating = True
Worksheets("simulateur").Protect "toto"
End Sub
 
Dernière modification par un modérateur:
VB:
Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets("simulateur").Unprotect "toto"
Application.ScreenUpdating = False
Application.EnableEvents = False

Select Case Target.Address(0, 0)
  Case Is = "K4"
    Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
    Range("S6") = Range("Y50")
  Case Is = "S6"
    Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
  Case Is = "Y6"
   Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
End Select

If Not Intersect(Target, Me.Range("F27")) Is Nothing Then
If Range("F27").Value < Range("B162").Value Then
MsgBox "1"
Else
'If Range("S6").Value <> Range("Q49").Value Then
MsgBox "2"
End If
End If

Application.EnableEvents = True
Application.ScreenUpdating = True
Worksheets("simulateur").Protect "toto"
End Sub
 
Dernière modification par un modérateur:
VB:
Private Sub Worksheet_Change(ByVal Target As Range)
'
    Worksheets("simulateur").Unprotect "toto"
    Application.ScreenUpdating = False
    Application.EnableEvents = False

    Select Case Target.Address(0, 0)
    Case "K4"
        Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
        Range("S6") = Range("Y50")
    Case "S6"
        Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
    Case "Y6"
        Range("F27").FormulaLocal = "=SIERREUR(RECHERCHEV(S6;A147:B162;2;Faux);""<10 ans"")"
    End Select

    If Not Intersect(Target, Me.Range("F27")) Is Nothing Then
        If Range("F27").Value < Range("B162").Value Then
            MsgBox "1"
        Else
'            If Range("S6").Value <> Range("Q49").Value Then
            MsgBox "2"
        End If
    End If

    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Worksheets("simulateur").Protect "toto"

End Sub
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

  • Question Question
Microsoft 365 worksheet_change
Réponses
29
Affichages
246
  • Question Question
Microsoft 365 Probléme VBA
Réponses
8
Affichages
230
Réponses
14
Affichages
357
Réponses
0
Affichages
536
Réponses
1
Affichages
318
Réponses
4
Affichages
358
Retour