0

我创建了这个宏,我在该行收到一个运行时错误“13”

Set objDomAttribute = objDomElement3.Attributes.setNamedItem(objDomDoc.createAttribute("Name4"))

我的 Excel 文件有 2727 行。我测试了更少的行并且它工作得很好,但是对于 2727 它不起作用。我应该怎么做才能解决这个问题?

Option Explicit
Public Sub MakeXMLWithDom() 

Dim Q As String 
Q = Chr$(34) 

Dim objDomDoc As New MSXML2.DOMDocument60 

objDomDoc.LoadXML ("<?xml version=" & Q & "1.0" & Q & "encoding=" & Q & "utf-8" & Q & "?>") 

Dim objDomElement As MSXML2.IXMLDOMElement 
Dim objDomAttribute As MSXML2.IXMLDOMAttribute 
Dim nodeSet As MSXML2.IXMLDOMNodeList 

Set objDomElement = objDomDoc.createElement("Containers") 
Set objDomElement = objDomDoc.appendChild(objDomElement) 

 'finding the amount of rows in the excel
Dim numofrows As Long 
numofrows = Worksheets("EXAMPLE").Range("E1").Offset(Worksheets("EXAMPLE").Rows.Count - 1, 0).End(xlUp).Row 

Dim iRow As Integer 

Dim Value11 As Variant 
Dim Value12 As Variant 
Dim Value13 As Variant 

Dim objDomElement1 As Variant
Dim objDomElement2 As Variant
Dim objDomElement3 As Variant

For iRow = 10 To (numofrows + 1) 

    If Worksheets("EXAMPLE").Cells(iRow - 1, 5) = Worksheets("EXAMPLE").Cells(iRow, 5) Then 
        Value11 = Worksheets("EXAMPLE").Cells(iRow - 1, 11).Value & ", " & Worksheets("EXAMPLE").Cells(iRow, 11).Value 
        Value12 = Worksheets("EXAMPLE").Cells(iRow - 1, 12).Value & ", " & Worksheets("EXAMPLE").Cells(iRow, 12).Value 
        Value13 = Worksheets("EXAMPLE").Cells(iRow - 1, 13).Value & ", " & Worksheets("EXAMPLE").Cells(iRow, 13).Value 
    Else 
        Value11 = Worksheets("EXAMPLE").Cells(iRow, 11).Value 
        Value12 = Worksheets("EXAMPLE").Cells(iRow, 12).Value 
        Value13 = Worksheets("EXAMPLE").Cells(iRow, 13).Value 
    End If 

    If Worksheets("EXAMPLE").Cells(iRow, 5) <> Worksheets("EXAMPLE").Cells(iRow + 1, 5) And Worksheets("EXAMPLE").Cells(iRow, 3) <> "" And Worksheets("EXAMPLE").Cells(iRow, 5) <> "" Then 

        Set objDomElement1 = objDomElement.appendChild(objDomDoc.createElement("Data")) 
        Set objDomAttribute = objDomElement1.Attributes.setNamedItem(objDomDoc.createAttribute("Relevant")) 
        objDomElement1.Attributes.getNamedItem("Relevant").Text = "True" 

        Set objDomElement2 = objDomElement1.appendChild(objDomDoc.createElement("Info")) 

        Set objDomElement3 = objDomElement2.appendChild(objDomDoc.createElement("Part")) 
        Set objDomAttribute = objDomElement3.Attributes.setNamedItem(objDomDoc.createAttribute("Name1")) 
        Set objDomAttribute = objDomElement3.Attributes.setNamedItem(objDomDoc.createAttribute("Name2")) 
        Set objDomAttribute = objDomElement3.Attributes.setNamedItem(objDomDoc.createAttribute("Name3")) 
        Set objDomAttribute = objDomElement3.Attributes.setNamedItem(objDomDoc.createAttribute("Name4")) 

        objDomElement3.Attributes.getNamedItem("Name1").Text = Trim$(Worksheets("EXAMPLE").Cells(iRow, 5).Value) 
        objDomElement3.Attributes.getNamedItem("Name2").Text = Value11 
        objDomElement3.Attributes.getNamedItem("Name3").Text = Value12 
        objDomElement3.Attributes.getNamedItem("Name4").Text = Value13 

    End If 
Next iRow 

objDomDoc.Save ("c:\Example.xml") 
End Sub
4

1 回答 1

2

您说Value13在 Locals 窗口中显示为Value: Error 2015Type: Variant/Error。好吧,好吧,继续调查……Value13从哪里来?查看该行的 Excel 工作表。我敢打赌你会在#VALUE!那里发现一个错误。

所以这是您输入数据的问题,而不是您的代码。解决这个问题,你的问题就会消失。

于 2012-11-29T09:35:06.670 回答