我是excel宏和VBA的新手,并且一直在努力解决以下问题。我在Excel中有以下数据集:
$('#ctl00_ContentPlaceHolder1_tvHierarchyView table tr td>a').click(function () {
var treeViewData = window["<%=tvHierarchyView.ClientID%>" + "_Data"];
if (treeViewData.selectedNodeID.value != "") {
var selectedNode=document.getElementById(treeViewData.selectedNodeID.value);
var value = selectedNode.href.substring(selectedNode.href.indexOf(",") + 3, selectedNode.href.length - 2);
var text = selectedNode.innerHTML;
alert("Text: " + text + "\r\n" + "Value: " + value);
} else {
alert("No node selected.")
}
$(this).removeAttr("href");
/// ...................... rest of your code
}); /// End of click function
}); /// End of document ready function
我希望用各自最高级别的根节点填充子节点,这样所有子节点都有一个根级别数据/父节点映射到它们,如下所示:
Parent Child
AAA BBB
AAA CCC
AAA DDD
BBB EEE
BBB FFF
CCC GGG
FFF HHH
III JJJ
JJJ KKK
JJJ LLL
我尝试创建一个VB函数来创建一个树但是无法从那里将它从最后或最高级别的根填充到相应的子节点。
我也尝试过使用逻辑,例如找到唯一的子名称和回溯来查找主根但无法这样做。
VB代码:
Node 1st Level Node
AAA Root
BBB AAA
CCC AAA
DDD AAA
EEE AAA
FFF AAA
GGG AAA
HHH AAA
III Root
JJJ III
KKK III
LLL III
我怎样才能继续这样做。
答案 0 :(得分:0)
假设父项在列A中,而子列在列B中,则以下宏将结果放在列D和E中
Option Explicit
Sub Root_Parent()
Dim i, re, k
i = 2
While Cells(i, 1) <> ""
Set re = Range("B:B").Find(Cells(i, 1))
If re Is Nothing Then
Set re = Range("D:D").Find(Cells(i, 1))
If re Is Nothing Then
k = k + 1
Cells(k, 4) = Cells(i, 1)
Cells(k, 5) = "Root"
findchild Cells(k, 4).Value, k
End If
End If
i = i + 1
Wend
End Sub
Sub findchild(parent, ByRef k)
Dim i, s, re
i = 2
While Cells(i, 2) <> ""
s = i
Do
Set re = Range("B:B").Find(Cells(s, 1))
If re Is Nothing Then
If Cells(s, 1) = parent Then
k = k + 1
Cells(k, 4) = Cells(i, 2)
Cells(k, 5) = Cells(s, 1)
End If
Exit Do
Else
s = re.Row
End If
Loop
i = i + 1
Wend
End Sub