1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, r As Range, f As String
Debug.Print Target.Address, IsNumeric(Target)
If Target.Address = "$Z$91" And IsNumeric(Target) Then 'si la cellule modifiée est bien Z16 et si la valeur est numérique
Application.ScreenUpdating = False 'Evite les scintillements de l'affichage et augmente la vitesse d'exécution.
Application.EnableEvents = False 'inhibe la détection de tout changement dans la feuille, ne rend pas la main au système
Range("X96:Y100000").Clear 'efface les précédents résultats
For i = 1 To Target.Value
'boucle de 1 jusqu'à la valeur saisie en Z16
Range(Cells(i + 95, 24), Cells(i + 95, 25)).MergeCells = True 'fusion des cellules
With Range(Cells(i + 95, 24), Cells(i + 95, 25))
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
End With
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
Next i 'boucle
Application.EnableEvents = True 'rend la main au système afin qu'il puisse détecter toute modification sur la feuille
Set r = Range(Cells(96, 24), Cells(Target.Value + 95, 24))
f = "=NBCAR(SUPPRESPACE(" & Cells(96, 24).Address(False, False) & "))=0"
With r
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:=f
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.Color = RGB(255, 199, 206)
End With
.FormatConditions(1).StopIfTrue = False
End With
Set r = Nothing
End If
End Sub |
Partager