我创造了这个宏和我在该行获得了运行时错误“13”运行时错误“13” VBA宏的Excel
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
这是“类型不匹配”错误。使用'Dim'声明所有*变量将帮助你找到这个错误的原因。例如,你还没有声明'objDomElement3'。您应该强制自己对所有变量执行此操作,只需在模块的顶部写入“Option Explicit”即可。 –
Corbett先生,我听取了您的建议,我添加了Option Explicit,我声明了objDomElements,但仍然在同一行发生同样的错误。 – Maggie11
当您在调试模式下遍历代码时,请查看本地窗口。该行涉及的所有变量的类型是什么?他们是否如预期? –