Option Explicit
Sub ORGANIGRAME2()
deleteshape
Dim L1&, L2&, L&, T, C1, C2, i, ecart&, haut&, shapeparent
Dim pere, shap, lenX&, larg, r As Range, Cel As Range, connecteur
T = 20
L1 = 650
larg = 40
ecart = 20
haut = 15
deleteshape
Set r = Range("B3:E" & Cells(Rows.Count, 2).End(xlUp).Row)
'LA TAILLE de texte LA PLUS GRANDE
For Each Cel In r.Columns(2).Cells
If Cel <> "" Then If lenX < (Len(Cel.Value) * 8) Then lenX = Len(Cel.Value) * 8
Next
If lenX > larg Then larg = lenX:
'le père
Set pere = ActiveSheet.Shapes.AddShape(msoShapeRectangle, L1, T, larg, haut)
pere.Name = r.Cells(1, 2)
pere.TextFrame.Characters.Text = r.Cells(1, 2)
pere.TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
pere.TextFrame2.VerticalAnchor = msoAnchorMiddle
pere.Fill.ForeColor.RGB = RGB(255, 0, 0)
ActiveSheet.DrawingObjects(pere.Name).Font.Size = 9
pere.OnAction = "actionshape"
'les enfant etage(2)
'L2 = L1 - ((WorksheetFunction.CountIf(r.Columns(3), "2") * (ecart)) / 2) - larg - ecart 'position left du premier enfant etage(2)
L2 = L1 - ((WorksheetFunction.CountIf(r.Columns(3), "2") * larg) / 2) 'position left du premier enfant etage(2)
For i = 2 To r.Rows.Count
If r.Cells(i, 3) = 2 Then
T = 20 * 2 + 3
Set shap = ActiveSheet.Shapes.AddShape(msoShapeRectangle, L2, T, larg, haut)
shap.Name = r.Cells(i, 2)
shap.TextFrame.Characters.Text = r.Cells(i, 2)
shap.TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
shap.TextFrame2.VerticalAnchor = msoAnchorMiddle
ActiveSheet.DrawingObjects(shap.Name).Font.Size = 9
ActiveSheet.DrawingObjects(shap.Name).Font.Color = vbBlue
shap.OnAction = "actionshape"
shap.Fill.ForeColor.RGB = vbGreen
L2 = L2 + larg + ecart
Set connecteur = ActiveSheet.Shapes.AddConnector(msoConnectorElbow, 100, 100, 100, 100)
connecteur.Name = "ba" & shap.Name
If shap.Left < pere.Left Then C1 = 3: C2 = 1 Else C1 = 3: C2 = 1
connecteur.ConnectorFormat.BeginConnect pere, C1
connecteur.ConnectorFormat.EndConnect shap, C2
connecteur.Line.ForeColor.RGB = vbRed
connecteur.Line.Weight = 2
End If
Next
'les arrières enfant etage(3)
For i = 1 To r.Rows.Count
If r.Cells(i, 3) = 2 Then
Set shapeparent = ActiveSheet.Shapes(r.Cells(i, 2))
T = shapeparent.Top
L = shapeparent.Left
End If
If r.Cells(i, 3) = 3 Then
T = T + 25
Set shap = ActiveSheet.Shapes.AddShape(msoShapeRectangle, L, T, larg, haut)
shap.Name = r.Cells(i, 2).Text
shap.TextFrame.Characters.Text = r.Cells(i, 2).Text
shap.TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
shap.TextFrame2.VerticalAnchor = msoAnchorMiddle
shap.Fill.ForeColor.RGB = RGB(200, 150, 0)
ActiveSheet.DrawingObjects(shap.Name).Font.Color = vbRed
ActiveSheet.DrawingObjects(shap.Name).Font.Size = 9
shap.Left = L + 5
shap.OnAction = "actionshape"
Set connecteur = ActiveSheet.Shapes.AddConnector(msoConnectorElbow, 100, 100, 100, 100)
connecteur.Name = "boo" & shap.Name
If shap.Left < shapeparent.Left Then C1 = 2: C2 = 2 Else C1 = 4: C2 = 4
connecteur.ConnectorFormat.BeginConnect shapeparent, C1
connecteur.ConnectorFormat.EndConnect shap, C2
connecteur.Width = connecteur.Width - 2
connecteur.Top = shapeparent.Top + haut
connecteur.Line.ForeColor.RGB = vbGreen
connecteur.Line.Weight = 3
End If
Next
End Sub
Sub actionshape()
MsgBox Application.Caller
End Sub
Sub deleteshape()
Dim shap
For Each shap In ActiveSheet.Shapes: shap.Delete: Next
End Sub