Private Sub CommandButton1_Click()
x = GetDimPositionShapeToRange([c4], Shapes("toto"), 5)
With Shapes("toto")
.Left = x(0): .Top = x(1): .Width = x(2): .Height = x(3)
End With
End Sub
Private Sub CommandButton2_Click()
x = GetDimPositionShapeToRange([c11], Shapes("toto"), 5)
With Shapes("toto")
.Left = x(0): .Top = x(1): .Width = x(2): .Height = x(3)
End With
End Sub
Function GetDimPositionShapeToRange(rng As Range, shap, Optional marge As Long = 0) 'la marge exprime un pourcentage de 1 à x%
Dim Ratio#, W1#, H1#, W2#, H2#, Tp#, Lt#
Ratio = Application.Min(rng.Cells(1).MergeArea.Width / shap.Width, rng.Cells(1).MergeArea.Height / shap.Height)
With shap
W1 = .Width: H1 = .Height
W2 = W1 * (Ratio - ((Ratio / 100) * marge))
H2 = H1 * (Ratio - ((Ratio / 100) * marge))
Tp = rng.Top + ((rng.Cells(1).MergeArea.Height - H2) / 2)
Lt = rng.Left + ((rng.Cells(1).MergeArea.Width - W2) / 2)
GetDimPositionShapeToRange = Array(Lt, Tp, W2, H2)
End With
End Function