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
| Sub compte()
Dim rngPlage As Range, rngCel As Range
Dim rngC As Range, i As Integer, j As Integer
Dim total As Integer
Set rngPlage = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
For Each rngCel In rngPlage
total = 0
For i = rngCel.Value - 1 To rngCel.Value + 1
Set rngC = rngPlage.Find(i, LookIn:=xlValues, lookat:=xlWhole)
If Not rngC Is Nothing Then
firstAddress = rngC.Address
Do
'pour i-1
If i = rngCel.Value - 1 Then
'vérifier j-1, j, j+1
If rngC.Offset(0, 1) >= rngCel.Offset(0, 1) - 1 And rngC.Offset(0, 1) <= rngCel.Offset(0, 1) + 1 Then
total = total + rngC.Offset(0, 2)
End If
'pour i
ElseIf i = rngCel.Value Then
'vérifier j-1, j+1
If rngC.Offset(0, 1) = rngCel.Offset(0, 1) - 1 Or rngC.Offset(0, 1) = rngCel.Offset(0, 1) + 1 Then
total = total + rngC.Offset(0, 2)
End If
'pour i+1
ElseIf i = rngCel.Value + 1 Then
'vérifier j-1, j, j+1
If rngC.Offset(0, 1) >= rngCel.Offset(0, 1) - 1 And rngC.Offset(0, 1) <= rngCel.Offset(0, 1) + 1 Then
total = total + rngC.Offset(0, 2)
End If
End If
Set rngC = rngPlage.FindNext(rngC)
Loop While Not rngC Is Nothing And rngC.Address <> firstAddress
End If
Next
rngCel.Offset(0, 3) = total
Next
End Sub |
Partager