代码之家  ›  专栏  ›  技术社区  ›  Pierre

Excel TreeView多列

  •  0
  • Pierre  · 技术社区  · 15 年前

    抱歉,我不喜欢Excel/VBA…来自Unix世界…我需要帮助!

    我正试图从一个包含3列的Excel工作表中构建一个Excel表单中的树视图。该表引用了已在“树视图”中组织的电影院列表,其中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
    

    按照这个描述,我希望TreeView看起来像:

    +-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 回复  |  直到 15 年前
        1
  •  0
  •   Adriaan Stander    15 年前

    看看这个。

    获取列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
    
        2
  •  0
  •   Pierre    15 年前

    谢谢你,阿斯坦德,你让我上路了…

    我们走到这里:

    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列值作为键,B列值作为childrennode的文本。

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

    -选择/取消选择父节点时,所有子节点都将被选择/取消选择。

    -我想让选定的数据存储在某个地方

    有人知道怎么做吗? 亲切的问候, 磷