VBA to create organisation chart

jbesclapez

Active Member
Joined
Feb 6, 2010
Messages
275
Hi there,

I found a great macro on the web that nearly does my job. But only nearly. Unfortunately this code is really high level, and I have no idea how to solve it. Super challenging!
You can download the file here:
orgaproblem
And what I would like to achieve is below: (see picture)
2020-06-12_10-56-36.png

Note that there is a limitation.
The children of G in the second table should only show once in the hierarchy otherwise it will be to messy.

2020-06-12_11-38-31.png



Thanks for your help
 
Do you have any idea of this error?

Yes. I need to know your Excel version.

I am using Excel 365 v1908 Semi Annual

I was also thinking if you send me the file directly, i should fine my way around VBA.

Thanks for sharing your work, it looks really great!
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
  • You can easily re-create my workbook; just paste the code, the source table, rename the worksheets and that is it.
  • As I think I know what the problem is, I will point you into the right direction.
  • Please turn the macro recorder on, insert the shape below at smart art> hierarchy and paste the resulting code here. I am showing what I got, I do not have Office 365 to test with.
  • Which one of my codes are you using? Note that the original thread has top to bottom and bottom to top charts, but the article only one option so far.

VBA Code:
' Excel 2016

Sub Macro1()
''
Call ActiveSheet.Shapes.AddSmartArt(Application.SmartArtLayouts( _
"urn:microsoft.com/office/officeart/2008/layout/NameandTitleOrganizationalChart")).Select
End Sub

smart_a.PNG
 
Upvote 0
And here is mine :

VBA Code:
Sub Macro2()
    Call ActiveSheet.Shapes.AddSmartArt(Application.SmartArtLayouts( _
        "urn:microsoft.com/office/officeart/2008/layout/NameandTitleOrganizationalChart" _
        )).Select

End Sub

I am trying to use your original code i found. That is the one not working on my office 365
 
Upvote 0
So our codes are the same...

But just to be sure, the error i am having is on
VBA Code:
Sub Phase1()                                            ' draw original chart
Dim arr(), i%, t
[B]arr = Range([a1].CurrentRegion.Address) [/B]                ' ERROR save original table

I copied your example data in a sheet called tdata, copy the VBA and created 2 other sheets secdata and fshap...

Could you send me a full working sheet? I have no idea what I am doing wrong !
 
Upvote 0
  • First of all, the source table goes on “fshap”, my post is not that clear on this aspect… this alone can solve the issue. The sheet names are not helping.
  • I can send you a test workbook, but before let´s check the version thing.
  • That recorded part is the same for 2016 and 365, this is important information.
  • Note that the original thread was written for Excel 2013, while the article supports 2013 and 2016. I am assuming you are using the article version.
  • What is the error message you get? The range is probably empty.
  • What do you get when running the code below? I would expect 16.

VBA Code:
Sub MyVer()

MsgBox Val(Application.Version)

End Sub
 
Upvote 0
Hi Worf,

It is version 16.

The macro is now working!
It took me a while to realize that i wrote "secdata " and not ''secdata" (note the space -and it drove me crazy!!!)

Now I need to find the macro for Top to bottom :-)

Please could you update your post for everybody! That would be really nice!

Have a good week end
 
Last edited:
Upvote 0
I can send you a test workbook, but before let´s check the version thing.
End Sub[/CODE]

Worf, could you still please send me the worksheet from TOP to BOTTOM. Thanks.
Also, i realized that you are probably better in Microsoft Excef than Microsoft Worf. - Sorry for the bad joke.
 
Upvote 0
Since we are at the bad jokes department, which rock star is great at spreadsheets?

Excel Rose.

Listen to me...


The code below is based on the original thread and creates a top to bottom chart with Excel 2016; I am planning to update the article tomorrow.

Note that for this example you need two sheets, named object and data2.

OrgaProblem.xlsm
ABCD
1SonFatherDescriptiondescription 1
2Fonction A1Fonction C0desc130%
3Fonction B2Fonction A1desc220%
4Fonction C2Fonction A1desc310%
5Fonction D2Fonction A1desc460%
6Fonction C1Fonction C0desc580%
7Fonction J1Fonction C1desc650%
8Fonction A2Fonction C1desc720%
9Fonction D1Fonction C0desc890%
10Fonction B2-2Fonction D1desc910%
11Fonction E1Fonction D1desc100%
12Fonction F1Fonction E1desc1130%
13Fonction G1Fonction E1desc1230%
data2


VBA Code:
Sub main()  ' run me
Adjust
CreateDiagram Sheets("object")
End Sub

Sub Adjust()
Dim lr%
Sheets("data2").Activate
[k:o].ClearContents
lr = Range("a" & Rows.Count).End(xlUp).Row
[k1] = "Seq": [L1] = "code1": [m1] = "code2"
[L2] = [b2]: [n1] = "info": [o1] = "info2"
[L2].Interior.Color = RGB(200, 50, 10)
[m2] = [b2]: [k2] = 2: [n2] = 0.01
[o2] = "desc0"
Range("a2:a" & lr).Copy [L3]
Range("b2:b" & lr).Copy [m3]
Range("c2:c" & lr).Copy [o3]
Range("d2:d" & lr).Copy [n3]
Range("k3:k" & lr + 1).Formula = "=row()"
End Sub

Sub CreateDiagram(Source As Worksheet)
Dim sal As SmartArtLayout, QNode As SmartArtNode, QNodes As SmartArtNodes, oshp As Shape, Line%, _
i%, r As Range, PID$, mn, mx, ws As Worksheet, dt As Worksheet, crar(), c%, ad, v, t, s As ShapeRange
c = 1
ReDim crar(1 To c)
Set ws = Sheets("object"): Set dt = Sheets("data2")
ws.Activate
ws.[a:f].ClearContents
dt.[k1].CurrentRegion.Copy ws.[a1]
For i = 1 To ws.Shapes.Count
    ws.Shapes(1).Delete
Next
Select Case Val(Application.Version)
    Case 15                                 ' Excel 2013
        Set sal = Application.SmartArtLayouts(89)
        Set oshp = ws.Shapes.AddSmartArt(sal)
    Case 16                                 ' Excel 2016
        Set oshp = ActiveSheet.Shapes.AddSmartArt(Application.SmartArtLayouts _
        ("urn:microsoft.com/office/officeart/2008/layout/NameandTitleOrganizationalChart"))
End Select
oshp.Top = [a50].Top
Set QNodes = oshp.SmartArt.AllNodes
For i = 1 To 5
    oshp.SmartArt.AllNodes(1).Delete        ' initial nodes
Next
Line = 2                                     ' look for roots
Do While Source.Cells(Line, 1) <> ""
    If Source.Cells(Line, 2) = Source.Cells(Line, 3) Then
        Set QNode = oshp.SmartArt.AllNodes.Add
        QNode.TextFrame2.TextRange.Text = Source.Cells(Line, 2)
        PID = Source.Cells(Line, 2)         ' parent node
        Source.Rows(Line).Delete
        AddChildNodes QNode, Source, PID
    Else
        Line = Line + 1
    End If
Loop
oshp.SmartArt.AllNodes(1).TextFrame2.TextRange.Text = dt.[L2]
oshp.Width = 1000
oshp.Height = 700
oshp.Select
CommandBars.ExecuteMso ("SmartArtConvertToShapes")
Selection.Ungroup
Set r = ws.[a2]
On Error Resume Next
For i = 1 To ws.Shapes.Count
    r = ws.Shapes(i).Height
    Set r = r.Offset(1)
Next
mn = WorksheetFunction.Min([a:a])
mx = WorksheetFunction.Max([a:a])
For i = ws.Shapes.Count To 1 Step -1
    If ws.Shapes(i).Height = mn Then ws.Shapes(i).Delete
    If ws.Shapes(i).Height = mx Then
        crar(c) = ws.Shapes(i).Name
        c = c + 1
        ReDim Preserve crar(1 To c)
    End If
Next
On Error GoTo 0
For i = LBound(crar) To UBound(crar)
    If Len(crar(i)) Then
        v = Split(ws.Shapes(crar(i)).TextFrame2.TextRange.Text, vbLf)(0)
        Set r = dt.Range("L:L").Find(v, dt.[L1], xlValues, 1)
        ad = r.Offset(, 2)
        ws.Shapes(crar(i)).Fill.ForeColor.RGB = r.Interior.Color
        Set s = ws.Shapes.Range(Array(crar(i)))
        s.TextFrame2.TextRange.Font.Bold = msoTrue
        s.TextFrame2.TextRange.Font.Name = "+mj-lt"
        ws.Shapes.AddShape(62, 10, 10, ws.Shapes(crar(i)).Width / 2.5, ws.Shapes(crar(i)).Height / 3).Name = _
        ws.Shapes(crar(i)).Name & "aux"
        With ws.Shapes(ws.Shapes(crar(i)).Name & "aux")
            .Left = ws.Shapes(crar(i)).Left
            .Top = ws.Shapes(crar(i)).Top + ws.Shapes(crar(i)).Height + 2
            .Line.ForeColor.SchemeColor = 1
            .Fill.Visible = msoFalse
            .TextFrame.Characters.Text = FormatPercent(ad, 0, vbTrue, vbFalse, vbUseDefault)
            .TextFrame.Characters(1, Len(ad)).Font.Size = 9
            .TextFrame.Characters(1, Len(ad)).Font.ColorIndex = 0
            .TextFrame.Characters(1, Len(ad)).Font.Bold = 1
            If ad = 0 Then .TextFrame.Characters.Text = "0%"
        End With
    End If
Next
End Sub

Sub AddChildNodes(QNode As SmartArtNode, Source As Worksheet, PID$)
Dim L%, Found As Boolean, ParNode As SmartArtNode, CurPid$, ad
L = 2
Found = False                           'nothing found yet
Do While Source.Cells(L, 1) <> ""
    If Source.Cells(L, 3) = PID Then
        Set ParNode = QNode
        Set QNode = QNode.AddNode(msoSmartArtNodeBelow)
        QNode.TextFrame2.TextRange.Text = Cells(L, 2) & vbLf & Cells(L, 5)
        CurPid = Source.Cells(L, 2)  ' current parent node
        If Not Found Then Found = True  'something was found
        Source.Rows(L).Delete
        AddChildNodes QNode, Source, CurPid
        Set QNode = ParNode
        ElseIf Found Then               'it's sorted, nothing else can be found
        Exit Do
    Else
        L = L + 1
    End If
Loop
End Sub
'****************
 
Upvote 0

Excel Rose .... :-) I should have guessed that one. Well played! LOL

I used the workbook you sent me.
The macro has a major limitation that makes it nearly unsuable. The weird thing, is that I do not think you had this limitation in the past. Let me explain :
The more members you add to the data2, the smaller the chart gets. And at the end it is so small that it makes it unreadable...
 
Upvote 0

Forum statistics

Threads
1,224,722
Messages
6,180,562
Members
452,987
Latest member
mrfitness_79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top