我正在尝试解析XML,需要将特定数据提取到Excel格式。 以下是我要解析的XML。我试图提高身份和博士

问题描述 投票:0回答:0

出于某种原因,我的代码正在丢弃此错误消息:运行时间错误91:对象变量或块变量未设置。

它突出显示代码:sheets(“ sheet1”)。单元格(i,4).value = mlfda.getElementsbytagname(“ status”)(0).Text

在下面看到我的整个代码:enter image description here

Sub PullComplexXML() Dim xmlDoc As Object Dim XMLPGA As Object Dim xmlfda As Object Dim i As Integer 'Dim xmlDoc As Object Set xmlDoc = CreateObject("MSXML2.DOMDocument") xmlDoc.Load ("C:\books.xml") ' Adjust "SectionName" Dim sectionNode As Object Set sectionNode = xmlDoc.SelectSingleNode("//PGAs") If Not sectionNode Is Nothing Then Dim childNode As Object For Each childNode In sectionNode.ChildNodes Debug.Print childNode.Text Next childNode Else MsgBox "Section not found" End If 'Pull nested data ' Create a new XML Document object Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0") ' Configure properties xmlDoc.async = False xmlDoc.validateOnParse = False ' Load the XML file If Not xmlDoc.Load("C:\books.xml") Then MsgBox "Failed to load XML file. Exiting." Exit Sub End If i = 2 'Loop through each PGA For Each XMLPGA In xmlDoc.DocumentElement.ChildNodes 'Loop through each FDA For Each xmlfda In XMLPGA.ChildNodes Sheets("Sheet1").Cells(i, 1).Value = xmlDoc.DocumentElement.getAttribute("Product") 'Product Sheets("Sheet1").Cells(i, 2).Value = XMLPGA.getAttribute("PGAs") ' PGA Sheets("Sheet1").Cells(i, 3).Value = xmlfda.getAttribute("FDA") ' FDA Sheets("Sheet1").Cells(i, 4).Value = xmlfda.getElementsByTagName("Status")(0).Text ' Status Sheets("Sheet1").Cells(i, 5).Value = xmlfda.getElementsByTagName("DisclaimReason")(0).Text ' DR i = i + 1 Next xmlfda Next XMLPGA Set xmlDoc = Nothing MsgBox "Data loaded. Now validate your file!" End Sub

我不会为您的VBA问题提供帮助,因为我相信您正在为此任务使用错误的工具。当Excel已经提供了用于有效处理此类方案的内置工具时,无需为所有可能的XML结构编写自定义VBA代码。
功率查询使您无需复杂的,容易出错的代码加载和转换XML文件
。它会自动检测层次结构,使您能够以最小的努力提取和操纵数据。
我强烈建议探索电源查询,而不是VBA。

excel vba xml
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.