Excel TreeView多列

时间:2009-12-01 09:20:49

标签: excel treeview

抱歉我的Noobness with Excel / Vba ...来自unix世界......我需要帮助!

我正在尝试从包含3列的Excel工作表构建Excel表单中的TreeView。该表格引用了一个已经在“树视图”中组织的电影院列表,其中A列是指电影院组名称,B和C列是指特定的电影院名称和信息,例如:

A1:Independent Cinemas
B2:CinemaName1 C2:Cinema1 Infos
B3:CinemaName2 C3:Cinema2 Infos
B4:CinemaName3 C4:Cinema3 Infos
A5:Cineplex Cinemas
B6:CinemaName4 C6:Cinema4 Infos
B7:CinemaName5 C7:Cinema5 Infos
A8:Next Group of Cinemas
B9:..... etc etc

根据此描述,我希望树视图看起来像:

+-A1
---+B2,C2
---+B3,C3
---+B4,C4
+-A5
---+B6,C6
---+B7,C7
+-A8
---+B9,C9
etc...

对不起表示蹩脚,但你得到的照片...... 以下是我到目前为止的情况:

Private Sub TreeView_Populate()

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

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

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

Dim lElement As Long
Dim rCell As Range

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

        For Each rCell In rngZones
            'We have a group name in the A columns so we attach it to the tree
            If Not rCell.Text = "" Then
                .Add Key:=rCell.Text, Text:=rCell.Text
                'THIS IS WHERE I BLOCK!!
                'Need the range from Columns B and C until the next Value in the A Column
                'in order to add the children nodes....
                        .Add relative:=CinemaName, _
                          relationship:=tvwChild, _
                          Key:=CinemaName(here it will be B column), CinemaInfos(C column)
                          Text:=CinemaName(B column)
            End If
        Next rCell
End With
End Sub

我在表单初始化子例程中也有.ZonesTree.LineStyle = tvwRootLines,它为树的每个元素创建复选框。我希望默认情况下选中所有复选框...

这可行吗?我基本上需要一个“临时”范围,包含来自B et C列的值来构建子节点...在vba代码中我添加了一些注释到我失败的地方...... 非常感谢所有帮助/建议!

2 个答案:

答案 0 :(得分:0)

看看这个。

获取列a的完整范围以循环行。同时获取B,C的范围并使用计数器指示您在哪一行

Private Sub TreeView_Populate()

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

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

    lngRows = wsZones.UsedRange.Rows.Count
    Set rngZones = wsZones.Range("A1:A" & lngRows)
    Dim rngBC As Range
    Set rngBC = wsZones.Range("B1:C" & lngRows)

    'lngRows = wsZones.Range("A65536").End(xlUp).Row
    'Set rngZones = wsZones.Range("A1:A" & lngRows)

    Dim lElement As Long
    Dim rCell As Range
    Dim rowCount As Integer
    rowCount = 1

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

            For Each rCell In rngZones
                'We have a group name in the A columns so we attach it to the tree
                If Not rCell.Text = "" Then
                    .Add Key:=rCell.Text, Text:=rCell.Text
                    'THIS IS WHERE I BLOCK!!
                    'Need the range from Columns B and C until the next Value in the A Column
                    'in order to add the children nodes....
                            .Add relative:=CinemaName, _
                              relationship:=tvwChild, _
                              Key:=CinemaName(here it will be B column), CinemaInfos(C column)
                              Text:=CinemaName(B column)
                End If
                'this is how to get the bc range
                Dim currentRowRange As Range
                Set currentRowRange = rngBC.Rows(rowCount)
                Dim b As String
                Dim c As String
                b = currentRowRange.Cells(, 1)
                c = currentRowRange.Cells(, 2)
                rowCount = rowCount + 1
            Next rCell
    End With
    End Sub

答案 1 :(得分:0)

谢谢旁观者,你让我在途中......

我们走了:

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.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

我实际上想要将C列值作为Key和B列值作为ChildrenNode的文本。

下一个问题,我正在使用此树的复选框模式,表示所有节点都是可选的。 我希望默认情况下: - 选中所有复选框

- 当您选择/取消选择父节点时,所有子节点都会被选中/取消选择

- 我希望获取所选数据以将其存储在某个地方

有没有人知道如何做到这一点? 亲切的问候, P