VBA Excel通过转换为Long来提供过低

时间:2017-12-21 14:15:07

标签: xml excel vba excel-vba

我是VBA的新手并且在excel上使用VBA并编写一个宏来每行导出一个xml文件(请参阅打印屏幕中的示例)。所有字段代表正确的格式,除了" SSCC"领域。这会返回溢出错误

我使用以下代码读取excel表并创建xml文件:

Sub CustomerOutToXML()

 sTemplateXML = _
        "<?xml version='1.0'?>" + vbNewLine + _
        "<ENVELOPE>" + vbNewLine + _
            "<TRANSACTION>" + vbNewLine + _
                "<TYPE>" + vbNewLine + "</TYPE>" + vbNewLine + _
            "</TRANSACTION>" + vbNewLine + _
            "<CONTENT>" + vbNewLine + vbNewLine + _
                "<DATE>" + vbNewLine + "</DATE>" + vbNewLine + _
                "<SSCC>" + vbNewLine + "</SSCC>" + vbNewLine + _
                "<ORDER>" + vbNewLine + "</ORDER>" + vbNewLine + _
            "</CONTENT>" + vbNewLine + _
        "</ENVELOPE>"

 Set doc = CreateObject("MSXML2.DOMDocument")
 doc.async = False
 doc.validateOnParse = False
 doc.resolveExternals = False

 With ActiveWorkbook.Worksheets(9)
  lLastRow = .UsedRange.Rows.Count

  For lRow = 2 To 10
   sFile = "T:\xxx\xxx\CustomerOutXML\CustomerOut" & .Cells(lRow, 1).Value & ".xml"
   Dim sDATE As String
   Dim sSSCC As Long
   Dim sORDER As String

   sDATE = CStr(.Cells(lRow, 2).Value)
   sSSCC = CLng(.Cells(lRow, 3).Value)
   sORDER = CStr(.Cells(lRow, 4).Value)
   sTransactionType = ActiveSheet.Name

   doc.LoadXML sTemplateXML
   doc.getElementsByTagName("DATE")(0).appendChild doc.createTextNode(sDATE)
   doc.getElementsByTagName("TYPE")(0).appendChild doc.createTextNode(sTransactionType)
   doc.getElementsByTagName("SSCC")(0).appendChild doc.createTextNode(sSSCC)
   doc.getElementsByTagName("ORDER")(0).appendChild doc.createTextNode(sORDER)


   doc.Save sFile
  Next

 End With

End Sub

EXcel文件示例 example of the excel format 能帮我解决这个问题吗

1 个答案:

答案 0 :(得分:2)

您需要将sSSCC声明为String。然后,不要将.Value与您的行sSSCC = .Cells(lRow, 3).Value一起使用,而是使用.Text代替(并删除CLng

Sub CustomerOutToXML()

 sTemplateXML = _
        "<?xml version='1.0'?>" + vbNewLine + _
        "<ENVELOPE>" + vbNewLine + _
            "<TRANSACTION>" + vbNewLine + _
                "<TYPE>" + vbNewLine + "</TYPE>" + vbNewLine + _
            "</TRANSACTION>" + vbNewLine + _
            "<CONTENT>" + vbNewLine + vbNewLine + _
                "<DATE>" + vbNewLine + "</DATE>" + vbNewLine + _
                "<SSCC>" + vbNewLine + "</SSCC>" + vbNewLine + _
                "<ORDER>" + vbNewLine + "</ORDER>" + vbNewLine + _
            "</CONTENT>" + vbNewLine + _
        "</ENVELOPE>"

 Set doc = CreateObject("MSXML2.DOMDocument")
 doc.async = False
 doc.validateOnParse = False
 doc.resolveExternals = False

 With ActiveWorkbook.Worksheets(9)
  lLastRow = .UsedRange.Rows.Count

  For lRow = 2 To 10
   sFile = "T:\xxx\xxx\CustomerOutXML\CustomerOut" & .Cells(lRow, 1).Value & ".xml"
   Dim sDATE As String
   Dim sSSCC As String  'Not Long
   Dim sORDER As String

   sDATE = CStr(.Cells(lRow, 2).Value)
   sSSCC = .Cells(lRow, 3).text ' <<< Not .Value
   sORDER = CStr(.Cells(lRow, 4).Value)
   sTransactionType = ActiveSheet.Name

   doc.LoadXML sTemplateXML
   doc.getElementsByTagName("DATE")(0).appendChild doc.createTextNode(sDATE)
   doc.getElementsByTagName("TYPE")(0).appendChild doc.createTextNode(sTransactionType)
   doc.getElementsByTagName("SSCC")(0).appendChild doc.createTextNode(sSSCC)
   doc.getElementsByTagName("ORDER")(0).appendChild doc.createTextNode(sORDER)


   doc.Save sFile
  Next

 End With

End Sub