Excel 从XML自动填充动态生成的工作表
所以我有一个XML文件,看起来像这样:Excel 从XML自动填充动态生成的工作表,excel,vba,Excel,Vba,所以我有一个XML文件,看起来像这样: <?xml version="1.0"?> <catalog> <query id="bk101"> <question>Do we have cloud security</question> <answer>Yes</answer> <genre>Cloud</genre> </query&g
<?xml version="1.0"?>
<catalog>
<query id="bk101">
<question>Do we have cloud security</question>
<answer>Yes</answer>
<genre>Cloud</genre>
</query>
<query id="bk102">
<question>Do we have locks on the door</question>
<answer>No, we have fingerprint access.</answer>
<genre>Physical Security</genre>
</query>
<query id="bk103">
<question>What SDLC Priciple is follwed?</question>
<answer>None</answer>
<comment>We have code ninjas!</comment>
<genre>SDLC</genre>
</query>
</catalog>
我在处理行计数器逻辑时遇到了困难,因此,我经常会得到索引越界错误。我该如何解决这个问题
谢谢。类似的测试:
Dim questions As MSXML2.IXMLDOMNodeList, question As MSXML2.IXMLDOMNode
Dim genre
Set questions = oXMLFile.SelectNodes("/catalog/query")
For Each question In questions
genre = ChildValue(question, "genre")
If Len(genre) > 0 Then
With eWorkbook.Sheets(genre).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
.Cells(1).Value = ChildValue(question, "question")
.Cells(2).Value = ChildValue(question, "answer")
.Cells(3).Value = ChildValue(question, "comment")
End With
End If
Next question
获取子节点值的实用程序函数:
Function ChildValue(n As MSXML2.IXMLDOMNode, childName As String)
Dim el, rv
Set el = n.SelectSingleNode(childName)
If Not el Is Nothing Then rv = el.nodeTypedValue
ChildValue = rv
End Function
1您有多张工作表,但只有一个行计数器。2您需要将工作表填充代码移到j循环之外,否则它将针对每个子元素运行。3在进入j回路之前,重置图纸名称、问题等。谢谢提醒。请你帮我解释一下循环逻辑和计数器逻辑好吗。我不需要精确的代码,但需要一些伪代码帮助。谢谢。我有一个用户定义类型未定义错误。我缺少哪些参考资料?microsoft XML v6库。谢谢。你能解释一下这个逻辑吗?我是VBA新手,发现很难理解eWorkbook.Sheetsgenre.CellsRows.Count,1.EndxlUp.Offset1,0.EntireRow CellsRows.Count,1从A列工作表底部开始。EndxlUp相当于按Ctrl+UpArrow键,并将您带到该列中最后一个已占用的单元格:您需要该列下方的第一个空单元格,因此添加Offset1,0以下拉一行。
Function ChildValue(n As MSXML2.IXMLDOMNode, childName As String)
Dim el, rv
Set el = n.SelectSingleNode(childName)
If Not el Is Nothing Then rv = el.nodeTypedValue
ChildValue = rv
End Function