Smart Art Org Chart

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Smart Art Org Chart

Post by SmallFry »

This thread is exactly what I'm looking for, but with maybe one deviation.

Instead of the WBS in column G, I will have codes like below. So the formula in Column I would be replaced with =LEN(G2)-1.

SS
SBA
SBAA
SBAC
SBB
SBBP
SBBX
SCL
SCLX
SCLY
SCLZ

I know the change is required in the AddChildren sub, but not sure how to change the v = Split(Code, ".") part to look at the level versus the WBS.

Code: Select all

Sub AddChildren(ByVal QParent As SmartArtNode, ByVal Code As String)
    Dim Level As Long
    Dim v As Variant
    Dim r As Long
    Dim QChild As SmartArtNode
    ' Dissect the code
    v = Split(Code, ".")
    ' Next level
    Level = UBound(v) + 2
    ' Loop through the rows
    For r = 2 To Range("A1").End(xlDown).Row
        ' Look for correct level and code
        If Range("C" & r).Value = Level And Range("G" & r + 1).Value Like Code & ".*" Then
            ' Create new node
            Set QChild = QParent.AddNode(msoSmartArtNodeBelow)
            ' Set node properties
            With QChild.TextFrame2.TextRange
                .Text = Range("B" & r).Value
                .Font.Fill.ForeColor.RGB = Range("C" & r).Font.Color
                .Font.Size = 7
                .Font.Italic = Range("C" & r).Font.Italic
                If Range("C" & r).Font.Underline = xlUnderlineStyleSingle Then
                    .Font.UnderlineColor = Range("C" & r).Font.Color    'hier wird die Schriftfarbe ?bernommen
                    .Font.UnderlineStyle = msoUnderlineSingleLine   'hier kommt unterstrichen?
                End If
                .Font.Strikethrough = Range("C" & r).Font.Strikethrough 'hier kommt durchgestrichen?
                '.Font.FontStyle = Range("C" & r).Font.FontStyle
                .Font.Bold = Range("C" & r).Font.Bold
            End With
            QChild.Shapes(1).Fill.ForeColor.RGB = Range("C" & r).Interior.Color
            ' Recursion!
            Call AddChildren(QChild, Range("G" & r + 1).Value)
        End If
    Next r
End Sub

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Smart Art Org Chart

Post by HansV »

Does this work? If not, please post a sample workbook.

Code: Select all

Sub AddChildren(ByVal QParent As SmartArtNode, ByVal Code As String)
    Dim Level As Long
    Dim r As Long
    Dim QChild As SmartArtNode
    ' Next level
    Level = Len(Code) + 1
    ' Loop through the rows
    For r = 2 To Range("A1").End(xlDown).Row
        ' Look for correct level and code
        If Range("C" & r).Value = Level And Range("G" & r + 1).Value Like Code & "*" Then
            ' Create new node
            Set QChild = QParent.AddNode(msoSmartArtNodeBelow)
            ' Set node properties
            With QChild.TextFrame2.TextRange
                .Text = Range("B" & r).Value
                .Font.Fill.ForeColor.RGB = Range("C" & r).Font.Color
                .Font.Size = 7
                .Font.Italic = Range("C" & r).Font.Italic
                If Range("C" & r).Font.Underline = xlUnderlineStyleSingle Then
                    .Font.UnderlineColor = Range("C" & r).Font.Color
                    .Font.UnderlineStyle = msoUnderlineSingleLine
                End If
                .Font.Strikethrough = Range("C" & r).Font.Strikethrough
                .Font.Bold = Range("C" & r).Font.Bold
            End With
            QChild.Shapes(1).Fill.ForeColor.RGB = Range("C" & r).Interior.Color
            ' Recursion!
            Call AddChildren(QChild, Range("G" & r + 1).Value)
        End If
    Next r
End Sub
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Smart Art Org Chart

Post by SmallFry »

No, that didn't seem to work. I've stepped thru the code and it never passes the If statement and jumps straight to the End If.

Code: Select all

If Range("C" & r).Value = Level And Range("G" & r + 1).Value Like Code & "*" Then
I can see it's not matching the level, but not sure how to fix it.

Here is the workbook.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Smart Art Org Chart

Post by HansV »

The problem is that the codes in column G don't provide a hierarchy: we cannot see from a value such as FSC or FSD that it is a "child" of CC.
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Smart Art Org Chart

Post by SmallFry »

Hi Hans,

But doesn't' the Level in Column I or Column C show the hierarchy, just without the period.

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Smart Art Org Chart

Post by HansV »

The reason for the complicated code in Stephan Sander's thread is that we couldn't rely on the items being ordered according to the hierarchy. We had to rely on the PSP in column G.
I'll try to come up with an alternative.
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Smart Art Org Chart

Post by HansV »

See the attached version.
ORGTest (5).xlsm
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Smart Art Org Chart

Post by SmallFry »

This works great. Thanks Hans.

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Smart Art Org Chart

Post by SmallFry »

Hi Hans,

In the attachment from post your last post, after the org chart is created, is it possible to move any box that start with CC as an assistant under CC? I guess this could be during the initial build if that works better.

In this case, can the CCF and CCQ be moved as an assistant under the CC?

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Smart Art Org Chart

Post by HansV »

I am very sorry, but I haven't been able to find out how to create an assistant using VBA.
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Smart Art Org Chart

Post by SmallFry »

Okay, that's fine. Appreciate it.

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Smart Art Org Chart

Post by SmallFry »

Hi Hans, with the attachment in post #7, how as an option could I build the chart with only say level 1 and level2?

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Smart Art Org Chart

Post by HansV »

Like this:

Code: Select all

Const MaxLevel = 2

Sub org()
    Dim shp As Shape
    Dim ogSALayout As SmartArtLayout
    Dim ogShp As Shape
    Dim QNodes As SmartArtNodes
    Dim QNode As SmartArtNode
    Dim t As Long
    Dim i As Long
    Dim r As Long

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.EnableEvents = False

    For Each shp In ActiveSheet.Shapes
        If shp.Type = msoSmartArt Then
            shp.Delete
        End If
    Next shp

    Set ogSALayout = Application.SmartArtLayouts("urn:microsoft.com/office/officeart/2005/8/layout/orgChart1")
    Set ogShp = ActiveSheet.Shapes.AddSmartArt(ogSALayout, 630, 36, 720, 630)
    Set QNodes = ogShp.SmartArt.AllNodes
    t = QNodes.Count

    ' Delete all nodes except one
    For i = 2 To t
        ogShp.SmartArt.Nodes(1).Delete
    Next i

    ' Set root node properties
    Set QNode = QNodes(1)
    With QNode.TextFrame2.TextRange
        .Text = Range("B1").Value
        .Font.Fill.ForeColor.RGB = vbBlack
        .Font.Size = 8
        .Font.Bold = True
    End With
    QNode.Shapes(1).Fill.ForeColor.RGB = Range("C1").Interior.Color

    ' Recursively add children nodes
    r = 1
    Call AddChildren(QNode, r)

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub

Sub AddChildren(ByVal QParent As SmartArtNode, ByVal r As Long)
    Dim Level As Long
    Dim s As Long
    Dim QChild As SmartArtNode
    ' Current level
    Level = Range("C" & r).Value
    If Level = MaxLevel Then Exit Sub
    s = r + 1
    Do While Range("C" & s).Value > Level And Range("C" & s).Value <> ""
        ' Look for correct level and code
        If Range("C" & s).Value = Level + 1 Then
            ' Create new node
            Set QChild = QParent.AddNode(msoSmartArtNodeBelow)
            ' Set node properties
            With QChild.TextFrame2.TextRange
                .Text = Range("B" & s).Value
                .Font.Fill.ForeColor.RGB = Range("C" & s).Font.Color
                .Font.Size = 7
                .Font.Italic = Range("C" & s).Font.Italic
                If Range("C" & s).Font.Underline = xlUnderlineStyleSingle Then
                    .Font.UnderlineColor = Range("C" & s).Font.Color
                    .Font.UnderlineStyle = msoUnderlineSingleLine
                End If
                .Font.Strikethrough = Range("C" & s).Font.Strikethrough
                .Font.Bold = Range("C" & s).Font.Bold
            End With
            QChild.Shapes(1).Fill.ForeColor.RGB = Range("C" & s).Interior.Color
            ' Recursion!
            Call AddChildren(QChild, s)
        End If
        s = s + 1
    Loop
End Sub
Change the constant at the beginning if you want to stop at another level.
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Smart Art Org Chart

Post by SmallFry »

This works great. Thank you again Hans.

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Smart Art Org Chart

Post by SmallFry »

Hi Hans, I'm back looking for another tweak if possible. I've tried to add this by stepping thru the code and seeing where it could be placed, but honestly, I get confused with the loop.

In this file, column L holds a check mark and it's those records I would like to have skipped in the making of the org chart.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Smart Art Org Chart

Post by HansV »

Change the line

Code: Select all

        If Range("C" & s).Value = Level + 1 Then
to

Code: Select all

        If Range("C" & s).Value = Level + 1 And Range("L" & s + 1) = "" Then
Best wishes,
Hans

SmallFry
StarLounger
Posts: 91
Joined: 02 Sep 2018, 23:12

Re: Smart Art Org Chart

Post by SmallFry »

Again, works great and much appreciated for your support.

Dinesh
NewLounger
Posts: 4
Joined: 04 May 2020, 12:22

Re: Smart Art Org Chart

Post by Dinesh »

HansV wrote:
08 Jan 2020, 12:28
I am very sorry, but I haven't been able to find out how to create an assistant using VBA.
I know this is an old thread, but for anyone who comes looking, this might be what you were looking for (I'm not sure): https://docs.microsoft.com/en-us/office ... rtnodetype

User avatar
Stefan_Sand
4StarLounger
Posts: 415
Joined: 29 Mar 2010, 11:50
Location: Vienna, Austria

Re: Smart Art Org Chart

Post by Stefan_Sand »

The reason for the complicated code in Stephan Sander's thread is that we couldn't rely on the items being ordered according to the hierarchy. We had to rely on the PSP in column G.
I'll try to come up with an alternative.

Hi Hans, the reason for the hierarchy in column G is, that it is based on Jeremy Modjeskas Incredible work on building WBS structures.
First, i build up the WBS structure, then i have copied it into the WBS worksheet, and based on this, i built up with your highly appreciated help a coloured project structure.

Indeed, a built up structure, based on WBS numbers (e.g 1, 1.1, 1.2, 1.2.1 ….) does not provide the cool workout to get an assistant shown in it - as in the provided solution -(i have to do this manually).

Stefan Sandauer