vba treeview填充显示不了节点内容 20
在网上找了一段代码填充treeview的代码,我把Workbooks跟sheets的名字都改成他这样的还是显示不了节点内容大家帮我看看哪里有错啊?'填充TreeView控...
在网上找了一段代码填充treeview的代码,我把Workbooks跟sheets的名字都改成他这样的还是显示不了节点内容 大家帮我看看哪里有错啊?
'填充TreeView控件
Sub treeViewPopulate()
Dim i As Long
Dim j As Long
Dim xNode As Node
Dim NodeKey As String
Dim strWksName As String
Dim objWks As Worksheet
strWksName = "节点数据"
Set objWks = Workbooks("PopulateTreeView.xlsm").Sheets(strWksName)
j = objWks.Range("A" & Rows.Count).End(xlUp).Row
With Me.TreeView1
'第一层次的节点
For i = 2 To j
Set xNode = .Nodes.Add
NodeKey = objWks.Range("A" & i).Text
With xNode
.Key = NodeKey
.Text = NodeKey
.Expanded = False
End With
Next i
j = objWks.Range("C" & Rows.Count).End(xlUp).Row
'其他节点
For i = 2 To j
Set xNode = .Nodes.Add(objWks.Range("B" & i).Text, tvwChild)
NodeKey = objWks.Range("C" & i).Text
With xNode
.Key = NodeKey
.Text = NodeKey
End With
Next i
End With
Set xNode = Nothing
Set objWks = Nothing
End Sub
http://www.excelperfect.com/index.php/2009/10/09/populatetreeview/comment-page-1/
就这是这里代码excel图片也有 展开
'填充TreeView控件
Sub treeViewPopulate()
Dim i As Long
Dim j As Long
Dim xNode As Node
Dim NodeKey As String
Dim strWksName As String
Dim objWks As Worksheet
strWksName = "节点数据"
Set objWks = Workbooks("PopulateTreeView.xlsm").Sheets(strWksName)
j = objWks.Range("A" & Rows.Count).End(xlUp).Row
With Me.TreeView1
'第一层次的节点
For i = 2 To j
Set xNode = .Nodes.Add
NodeKey = objWks.Range("A" & i).Text
With xNode
.Key = NodeKey
.Text = NodeKey
.Expanded = False
End With
Next i
j = objWks.Range("C" & Rows.Count).End(xlUp).Row
'其他节点
For i = 2 To j
Set xNode = .Nodes.Add(objWks.Range("B" & i).Text, tvwChild)
NodeKey = objWks.Range("C" & i).Text
With xNode
.Key = NodeKey
.Text = NodeKey
End With
Next i
End With
Set xNode = Nothing
Set objWks = Nothing
End Sub
http://www.excelperfect.com/index.php/2009/10/09/populatetreeview/comment-page-1/
就这是这里代码excel图片也有 展开
2个回答
展开全部
经过我的测试,结果成功,现告诉你问题所在:
1、他的代码是一个过程,必须在窗体中相关的命令下来激活它才行;
随便放在一个窗体或控件下的事件中即可——如:
Private Sub UserForm_Initialize()
Dim i As Long
Dim j As Long
Dim xNode As Node
Dim NodeKey As String
Dim strWksName As String
Dim objWks As Worksheet
strWksName = "节点数据"
Set objWks = ThisWorkbook.Sheets(strWksName)
j = objWks.Range("A" & Rows.Count).End(xlUp).Row
With Me.TreeView1
'第一层次的节点
For i = 2 To j
Set xNode = .Nodes.Add
NodeKey = objWks.Range("A" & i).Text
With xNode
.Key = NodeKey
.Text = NodeKey
.Expanded = False
End With
Next i
j = objWks.Range("C" & Rows.Count).End(xlUp).Row
'其他节点
For i = 2 To j
Set xNode = .Nodes.Add(objWks.Range("B" & i).Text, tvwChild)
NodeKey = objWks.Range("C" & i).Text
With xNode
.Key = NodeKey
.Text = NodeKey
End With
Next i
End With
Set xNode = Nothing
Set objWks = Nothing
End Sub
2、注意工作簿的保存名称,如果保存的与他的引用不同,则可以通过改变其中的:
Set objWks = Workbooks("PopulateTreeView.xlsm").Sheets(strWksName)
为:
Set objWks = ThisWorkbook.Sheets(strWksName)
来通用!
祝你早日测试成功,这个代码并不太难……呵呵……
1、他的代码是一个过程,必须在窗体中相关的命令下来激活它才行;
随便放在一个窗体或控件下的事件中即可——如:
Private Sub UserForm_Initialize()
Dim i As Long
Dim j As Long
Dim xNode As Node
Dim NodeKey As String
Dim strWksName As String
Dim objWks As Worksheet
strWksName = "节点数据"
Set objWks = ThisWorkbook.Sheets(strWksName)
j = objWks.Range("A" & Rows.Count).End(xlUp).Row
With Me.TreeView1
'第一层次的节点
For i = 2 To j
Set xNode = .Nodes.Add
NodeKey = objWks.Range("A" & i).Text
With xNode
.Key = NodeKey
.Text = NodeKey
.Expanded = False
End With
Next i
j = objWks.Range("C" & Rows.Count).End(xlUp).Row
'其他节点
For i = 2 To j
Set xNode = .Nodes.Add(objWks.Range("B" & i).Text, tvwChild)
NodeKey = objWks.Range("C" & i).Text
With xNode
.Key = NodeKey
.Text = NodeKey
End With
Next i
End With
Set xNode = Nothing
Set objWks = Nothing
End Sub
2、注意工作簿的保存名称,如果保存的与他的引用不同,则可以通过改变其中的:
Set objWks = Workbooks("PopulateTreeView.xlsm").Sheets(strWksName)
为:
Set objWks = ThisWorkbook.Sheets(strWksName)
来通用!
祝你早日测试成功,这个代码并不太难……呵呵……
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询