运行时错误'13'VBA宏Excel

时间:2012-11-29 07:47:08

标签: excel vba runtime-error

我创建了这个宏,并在行

处得到运行时错误“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

1 个答案:

答案 0 :(得分:2)

您说“本地”窗口中的Value13显示为Value: Error 2015Type: Variant/Error。好吧,好吧,继续调查...... Value13来自哪里?查看该行上的Excel工作表。我打赌你会在那里发现#VALUE!错误。

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