我创建了这个宏,我在该行收到一个运行时错误“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