Option Explicit
Sub UnTrucQuiSertArien()
Const milisec = 50
Dim xcell, i&, j&, poids, xbary, ybary, n
With Selection
For Each xcell In .Cells
If xcell <> "" Then
If IsNumeric(xcell) Then
If xcell > 0 Then
If n = 0 Then
n = n + 1
poids = xcell
xbary = xcell.Left + xcell.Width / 2
ybary = xcell.Top + xcell.Height / 2
.Parent.Shapes("Gravite").Left = xbary - .Parent.Shapes("Gravite").Width / 2
.Parent.Shapes("Gravite").Top = ybary - .Parent.Shapes("Gravite").Height / 2
Attente milisec
Else
n = n + 1
xbary = (poids * xbary + xcell * (xcell.Left + xcell.Width / 2)) / (poids + xcell)
ybary = (poids * ybary + xcell * (xcell.Top + xcell.Height / 2)) / (poids + xcell)
poids = poids + xcell
.Parent.Shapes("Gravite").Left = xbary - .Parent.Shapes("Gravite").Width / 2
.Parent.Shapes("Gravite").Top = ybary - .Parent.Shapes("Gravite").Height / 2
Attente milisec
End If
End If
End If
End If
Next xcell
End With
End Sub
Sub Demo()
Range("A1:C10,J10:K21,A23:C30,A23:D29,D30").Select
UnTrucQuiSertArien
Attente 500
Range("A1:A30,K1:K30").Select
UnTrucQuiSertArien
Attente 500
Range("A1:C9,D10:F19,G20:I25,J26:K30").Select
UnTrucQuiSertArien
Attente 500
Range("B2:D9,E10:G18,K19:K30,J19:K30").Select
UnTrucQuiSertArien
MsgBox "Fini!"
End Sub
Sub Attente(Xmillisec&)
Dim T0#, T1#, T2#
T0 = Timer: T2 = T0 + Xmillisec / 1000#
Do
DoEvents: T1 = Timer: If T1 < T0 Then T1 = T1 + 86400
Loop Until T1 >= T2
End Sub