Excel se ferme avec macro If Not
B
Bonjour,
Avant tout je début en VBA, et n'ai pas trouvé le pourquoi du comment en recherchant.
J'utilise ce code que j'ai adapté pour modifier la valeur de certaines cellules via vba.
Mais le code fait planter Excel, depuis l'ajout des dernières modifications:
Ajout des If Not et plusieurs Range en KeyCells.
Auriez vous une piste?
Merci
Sub resetDataPhone()
If [C15].Value = "-" Then
[C17].Value = " "
End If
If [D15].Value = "-" Then
[D17].Value = " "
End If
If [E15].Value = "-" Then
[E17].Value = " "
End If
If [B22].Value = "-" Then
[B24].Value = " "
End If
If [C22].Value = "-" Then
[C24].Value = " "
End If
If [D22].Value = "-" Then
[D24].Value = " "
End If
If [E22].Value = "-" Then
[E24].Value = " "
End If
If [D9].Value = "-" Then
[E9].Value = " "
[E10].Value = " "
[E11].Value = " "
[E12].Value = " "
End If
If Not [C16].Value Like "*Phone*" Then
[C19].Value = " "
End If
If Not [D16].Value Like "*Phone*" Then
[D19].Value = " "
End If
If Not [E16].Value Like "*Phone*" Then
[E19].Value = " "
End If
If Not [B23].Value Like "*Phone*" Then
[B26].Value = " "
End If
If Not [C23].Value Like "*Phone*" Then
[C26].Value = " "
End If
If Not [D23].Value Like "*Phone*" Then
[D26].Value = " "
End If
If Not [E23].Value Like "*Phone*" Then
[E26].Value = " "
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("C15,C17,D9,D15,D17,E15,E17,B22,B24,C22,C24,D22,D24,E22,E24")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
resetDataPhone
End If
End SubBonjour,
essaie ainsi :
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("C15,C17,D9,D15,D17,E15,E17,B22,B24,C22,C24,D22,D24,E22,E24")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Application.EnableEvents = False
resetDataPhone
Application.EnableEvents = True
End If
End SubB
Cela fonctionne
Un tout grand merci