views:

345

answers:

1

I have a TreeView in excel 2007 with checkboxes.

  • I want the checkboxes to be all selected when the tree is populated
  • I want that when I select/unselect a parent node of the list, all its children are selected/unselected

Here is the code I have written so far:

Private Sub UserForm_Initialize()

 'Set control defaults
 With Me
      .CommandButton1.Caption = "Close"
      .Label1 = vbNullString
      .ZonesTree.LineStyle = tvwRootLines
 End With

 'Populate the Treeview
 Call TreeView_Populate

End Sub

Private Sub TreeView_Populate()

Dim wbBook As Workbook
Dim wsZones As Worksheet
Dim rngZones As Range
Dim rngCinemas As Range
Dim lngRows As Long

Set wbBook = ThisWorkbook
Set wsZones = wbBook.Worksheets("Cinemas")

'lngRows = wsZones.Range("A65536").End(xlUp).row
lngRows = wsZones.UsedRange.Rows.Count
Set rngZones = wsZones.Range("A1:A" & lngRows)

Dim rngBC As Range
Set rngBC = wsZones.Range("B1:C" & lngRows)


Dim rCell As Range
Dim lastCreatedKey As String
Dim rowCount As Integer
Dim currentRowRange As Range
rowCount = 1
lastCreatedKey = ""

With Me.ZonesTree.Nodes
      'Clear TreeView control
      .Clear

        For Each rCell In rngZones
            If Not rCell.Text = "" Then
                .Add Key:=rCell.Text, Text:=rCell.Text
                lastCreatedKey = rCell.Text
            Else
                Set currentRowRange = rngBC.Rows(rowCount)
                .Add Relative:=lastCreatedKey, relationship:=tvwChild, Key:=currentRowRange.Cells(, 2).Text, Text:=currentRowRange.Cells(, 1).Text
            End If
            rowCount = rowCount + 1
        Next rCell
End With
End Sub

Private Sub Treeview1_NodeClick(ByVal Node As MSComctlLib.Node)
 Me.Label1.Caption = Node.Key
End Sub


Private Sub CommandButton1_Click()
 Unload Me
End Sub

This tree picks data from a sheet in this manner:

---A---------B---------C---------D

ParentNode

------------ChildNode

------------ChildNode

------------ChildNode

ParentNode

------------ChildNode

------------ChildNode

ParentNode

------------ChildNode

etc... (you get the idea, its an excel sheet...)

What is the vba code to select/unselect the boxes?? I have been searching a lot and couldn't find an answer to this easy problem....

Thx in advance!

A: 

On access which I assume uses the same control the property you use is .checked

Just set that to true for the boxes you want ticked

Here is a code sample

Set iNode = objTree.Nodes.Add(strParent,tvwChild, strKey, strText)
iNode.Checked = true

You should be to adapt it to work in excel

Kevin Ross
this is what I thought but I couldnt find the right syntax for it, can you give me an example?
Piero
Code sample added to my first post, hope that helps
Kevin Ross
thanks I will work on it tomorrow and post my results!
Piero
Worked perfectly!
Piero