根据列的值写入不同的文件

时间:2017-12-05 11:41:03

标签: excel vba

我有一个用于工作的宏,我从Excel文件中的数据创建XML文件。工作表的第一列名为“修饰符”,此列中的值可以是“创建”,“删除”或“修改”。我目前正在写同一个文件,而不是从哪个修饰符。我想要做的是创建三个不同的文件,并在修饰符的功能中写入它们。 因此,如果修饰符是“create”,则写入Create.xml,如果修饰符为“delete”,则写入Delete.xml,如果修饰符为“modify”,则写入Modify.xml。

这是我的VBA脚本(出于某种原因,我无法将所有代码格式化为代码):

Sub Externas3g2gxml() 

Dim SpecAtt As String

SpecAtt = ThisWorkbook.Sheets("ExternalGSMCell").Cells(2, 1)


On Error Resume Next
    MkDir ThisWorkbook.Path & "\NR_Scripts_"
On Error GoTo 0

Const ModifRange As String = "B2:B5000"

    Dim Modif As range

    For Each Modif In range(ModifRange)
        If Modif.Value = "create" Then

           Sheets("ExternalGSMCell").Select

            strExt3g2gCreate = ThisWorkbook.Path & "\NR_Scripts_\Create_ExternalGSMCell.xml"

            f = FreeFile

            i = 4

            Open strExt3g2gCreate For Output As #f

            Version = ThisWorkbook.Sheets("ExternalGSMCell").Cells(2, 21)
            Print #f, "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no""?>"
            Print #f, "<bulkCmConfigDataFile xmlns:un=""utranNrm.xsd"" xmlns:xn=""genericNrm.xsd"" xmlns:gn=""geranNrm.xsd"" xmlns=""configData.xsd"" xmlns:es=""EricssonSpecificAttributes.17.09.xsd"">"
            Print #f, "<fileHeader fileFormatVersion=""32.615 V4.5"" vendorName=""Ericsson""/>"
            Print #f, "<configData>"
            Print #f, "<xn:SubNetwork id=""ONRM_ROOT_MO_R"">"

            While ThisWorkbook.Sheets("ExternalGSMCell").Cells(i, 2) <> ""
            If ThisWorkbook.Sheets("ExternalGSMCell").Cells(i, 2) <> "" Then

            Print #f, "             <gn:ExternalGsmCell id=""" & Cells(i, 2).Value & """ modifier=""create"">"
            Print #f, "                 <gn:attributes>"
            Print #f, "                     <gn:userLabel>" & Cells(i, 2).Value & "</gn:userLabel>"
            Print #f, "                     <gn:cellIdentity>" & Cells(i, 3).Value & "</gn:cellIdentity>"
            Print #f, "                     <gn:bcchFrequency>" & Cells(i, 4).Value & "</gn:bcchFrequency>"
            Print #f, "                     <gn:ncc>" & Cells(i, 5).Value & "</gn:ncc>"
            Print #f, "                     <gn:bcc>" & Cells(i, 6).Value & "</gn:bcc>"
            Print #f, "                     <gn:lac>" & Cells(i, 7).Value & "</gn:lac>"
            Print #f, "                     <gn:mcc>" & Cells(i, 9).Value & "</gn:mcc>"
            Print #f, "                     <gn:mnc>" & Cells(i, 10).Value & "</gn:mnc>"
            Print #f, "                 </gn:attributes>"
            Print #f, "                 <xn:VsDataContainer id=""" & Cells(i, 2).Value & """ modifier=""create"">"
            Print #f, "                     <xn:attributes>"
            Print #f, "                         <xn:vsDataType>vsDataExternalGsmCell</xn:vsDataType>"
            Print #f, "                         <xn:vsDataFormatVersion>EricssonSpecificAttributes.17.09</xn:vsDataFormatVersion>"
            Print #f, "                         <es:vsDataExternalGsmCell>"
            Print #f, "                             <es:maxTxPowerUl>" & Cells(i, 11).Value & "</es:maxTxPowerUl>"
            Print #f, "                             <es:qRxLevMin>" & Cells(i, 12).Value & "</es:qRxLevMin>"
            Print #f, "                             <es:individualOffset>0</es:individualOffset>"

            Print #f, "                             <es:mncLength>2</es:mncLength>"
            Print #f, "                             <es:bandIndicator>2</es:bandIndicator>"
            If ThisWorkbook.Sheets("ExternalGSMCell").Cells(i, 8) <> "" Then
            Print #f, "                             <es:rac>" & Cells(i, 8).Value & "</es:rac>"
            Else
            Print #f, "                             <es:rac>1</es:rac>"
            End If
            Print #f, "                         </es:vsDataExternalGsmCell>"
            Print #f, "                     </xn:attributes>"
            Print #f, "                 </xn:VsDataContainer>"
            Print #f, "             </gn:ExternalGsmCell>"
            End If

                    i = i + 1
                    m = m + 1

            Wend
            Print #f, "     </xn:SubNetwork>"
            Print #f, "   </configData>"
            Print #f, "  <fileFooter dateTime=""2017""/>"
            Print #f, "</bulkCmConfigDataFile>"
            Close f


        Else
            If Modif.Value = "update" Then
            strExt3g2gUpdate = ThisWorkbook.Path & "\NR_Scripts_\Update_ExternalGSMCell.xml"

            f = FreeFile

            i = 4
            m = 4

            Open strExt3g2gUpdate For Output As #f

            Version = ThisWorkbook.Sheets("ExternalGSMCell").Cells(2, 21)
            Print #f, "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no""?>"
            Print #f, "<bulkCmConfigDataFile xmlns:un=""utranNrm.xsd"" xmlns:xn=""genericNrm.xsd"" xmlns:gn=""geranNrm.xsd"" xmlns=""configData.xsd"" xmlns:es=""EricssonSpecificAttributes.17.09.xsd"">"
            Print #f, "<fileHeader fileFormatVersion=""32.615 V4.5"" vendorName=""Ericsson""/>"
            Print #f, "<configData>"
            Print #f, "<xn:SubNetwork id=""ONRM_ROOT_MO_R"">"

            While ThisWorkbook.Sheets("ExternalGSMCell").Cells(i, 2) <> ""
            If ThisWorkbook.Sheets("ExternalGSMCell").Cells(i, 2) <> "" Then

            Print #f, "             <gn:ExternalGsmCell id=""" & Cells(i, 2).Value & """ modifier=""update"">"
            Print #f, "                 <gn:attributes>"
            Print #f, "                     <gn:userLabel>" & Cells(i, 2).Value & "</gn:userLabel>"
            Print #f, "                     <gn:cellIdentity>" & Cells(i, 3).Value & "</gn:cellIdentity>"
            Print #f, "                     <gn:bcchFrequency>" & Cells(i, 4).Value & "</gn:bcchFrequency>"
            Print #f, "                     <gn:ncc>" & Cells(i, 5).Value & "</gn:ncc>"
            Print #f, "                     <gn:bcc>" & Cells(i, 6).Value & "</gn:bcc>"
            Print #f, "                     <gn:lac>" & Cells(i, 7).Value & "</gn:lac>"
            Print #f, "                     <gn:mcc>" & Cells(i, 9).Value & "</gn:mcc>"
            Print #f, "                     <gn:mnc>" & Cells(i, 10).Value & "</gn:mnc>"
            Print #f, "                 </gn:attributes>"
            Print #f, "                 <xn:VsDataContainer id=""" & Cells(i, 2).Value & """ modifier=""update"">"
            Print #f, "                     <xn:attributes>"
            Print #f, "                         <xn:vsDataType>vsDataExternalGsmCell</xn:vsDataType>"
            Print #f, "                         <xn:vsDataFormatVersion>EricssonSpecificAttributes.17.09</xn:vsDataFormatVersion>"
            Print #f, "                         <es:vsDataExternalGsmCell>"
            Print #f, "                             <es:maxTxPowerUl>" & Cells(i, 11).Value & "</es:maxTxPowerUl>"
            Print #f, "                             <es:qRxLevMin>" & Cells(i, 12).Value & "</es:qRxLevMin>"
            Print #f, "                             <es:individualOffset>0</es:individualOffset>"

            Print #f, "                             <es:mncLength>2</es:mncLength>"
            Print #f, "                             <es:bandIndicator>2</es:bandIndicator>"
            If ThisWorkbook.Sheets("ExternalGSMCell").Cells(i, 8) <> "" Then
            Print #f, "                             <es:rac>" & Cells(i, 8).Value & "</es:rac>"
            Else
            Print #f, "                             <es:rac>1</es:rac>"
            End If
            Print #f, "                         </es:vsDataExternalGsmCell>"
            Print #f, "                     </xn:attributes>"
            Print #f, "                 </xn:VsDataContainer>"
            Print #f, "             </gn:ExternalGsmCell>"
            End If

                    i = i + 1
                    m = m + 1

            Wend
            Print #f, "     </xn:SubNetwork>"
            Print #f, "   </configData>"
            Print #f, "  <fileFooter dateTime=""2017""/>"
            Print #f, "</bulkCmConfigDataFile>"
            Close f
            End If
        End If
        Next

End Sub

提前谢谢!

0 个答案:

没有答案