Check/Decheck nodes in TreeView VBA Excel

Posted on 2011-10-13
Last Modified: 2012-05-12
How can I Check/Decheck all nodes in treeView depending of selected node...
For example...

If I check Parent1 I would like to check also all below (Parent2, Child1,Child2,Child3)...
Also, the same should be applied as for deselect....
I would appreciate help a lot
Question by:alsam
    LVL 85

    Accepted Solution

    For example, assuming a Treeview called tvwMain:
    Private Sub tvwMain_NodeCheck(ByVal Node As MSComctlLib.Node)
          CheckNodes Node, Node.Checked
    End Sub
    Private Sub CheckNodes(ByRef oParentNode As Node, ByVal bChecked As Boolean)
     Dim oNode As Node
     ' Get the first child node
     Set oNode = oParentNode.Child
     ' Loop through the child nodes of this node
     ' until there are none left...
     Do While Not oNode Is Nothing
       ' Check/Uncheck the node
       oNode.Checked = bChecked
       ' Call this function again for the
       ' child node, so that its child nodes
       ' can get checked/unchecked.
       CheckNodes oNode, bChecked
       ' Get the next child node of this node
       Set oNode = oNode.Next
    End Sub

    Open in new window

    LVL 1

    Author Closing Comment

    Helped me a lot...


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
    This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
    The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
    The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now