从Excel获取记录标签到XML文件,使用Visual Basic Application脚本

huangapple go评论80阅读模式
英文:

Getting Record Tags from Excel to an XML file using Visual Basic Application script

问题

我是新手对VBA一无所知,我有一个可以将Excel文件转换为XML文件的脚本。我需要更改它,使其不再使用预定义的记录标签名称,而是使用Excel数据表中提供的数据。

如果我们以这个Excel表格为例,生成的XML文件将如下所示:

  1. <DeclarationFile>
  2. <ID>
  3. <k2>5555.555</k2>
  4. <k7>2222.222</k7>
  5. <k15>33.333</k15>
  6. </ID>
  7. <ID>
  8. <k2>4444.444</k2>
  9. <k7>1111.111</k7>
  10. <k15>66.666</k15>
  11. </ID>
  12. <ID>
  13. <k2>333.33</k2>
  14. <k7>5.55</k7>
  15. <k15>7.77</k15>
  16. </ID>
  17. </DeclarationFile>

但我需要更改代码,使其使用R14、R17等而不是ID。

整个代码如下:

  1. Sub CreateXMLFile()
  2. ' ...(此处省略了一些代码)...
  3. mn_XML_Record_Name = GapFiller(InputBox("2. 输入记录标签名称:", "CreateXMLFile", "ID"))
  4. ' ...(此处省略了一些代码)...
  5. For MN_Row = MN_DataRange(mn_second_range, 1) To MN_DataRange(mn_second_range, 2)
  6. Print #1, "<" & mn_XML_Record_Name & ">"
  7. ' ...(此处省略了一些代码)...
  8. Print #1, "</" & mn_XML_Record_Name & ">"
  9. Next MN_Row
  10. ' ...(此处省略了一些代码)...
  11. End Sub

如果你需要将代码更改为使用R14、R17等而不是ID,你可以将mn_XML_Record_Name的值更改为相应的标签名称。这样,XML文件将使用R14、R17等作为记录标签名称。不过,请确保你的Excel数据表中包含相应的数据。希望这可以帮助你入门VBA。

英文:

I'm brand new to VBA and I have got a script that turns an Excel into a XML file.
What I need is to change it so that rather using a predefined Record Tag name, it uses data given in the Excel data table.

从Excel获取记录标签到XML文件,使用Visual Basic Application脚本

If we take this Excel table as an example, the XML file would end up being:

  1. DeclarationFile&gt;
  2. &lt;ID&gt;
  3. &lt;k2&gt;5555.555&lt;/k2&gt;
  4. &lt;k7&gt;2222.222&lt;/k7&gt;
  5. &lt;k15&gt;33.333&lt;/k15&gt;
  6. &lt;/ID&gt;
  7. &lt;ID&gt;
  8. &lt;k2&gt;4444.444&lt;/k2&gt;
  9. &lt;k7&gt;1111.111&lt;/k7&gt;
  10. &lt;k15&gt;66.666&lt;/k15&gt;
  11. &lt;/ID&gt;
  12. &lt;ID&gt;
  13. &lt;k2&gt;333.33&lt;/k2&gt;
  14. &lt;k7&gt;5.55&lt;/k7&gt;
  15. &lt;k15&gt;7.77&lt;/k15&gt;
  16. &lt;/ID&gt;
  17. &lt;/DeclarationFile&gt;

But I need to change the code so that it uses the R14, R17 etc instead of ID

The whole code:

  1. Sub CreateXMLFile()
  2. Dim MN_Row As Integer, MN_Column As Integer, MN_TEMP As String, mn_YesOrNo As Variant, mndefine_folder As String
  3. Dim mn_XML_FileName As String, mn_XML_Record_Name As String, mn_LF As String, mn_rtc1 As Integer
  4. Dim mn_first_range As String, mn_second_range As String, mn_tt As String, mn_FieldName(99) As String
  5. mn_LF = Chr(10) &amp; Chr(13)
  6. mndefine_folder = &quot;C:\&quot;
  7. mn_YesOrNo = MsgBox(&quot;Vajadzigs:&quot; &amp; mn_LF _
  8. &amp; &quot;1. XML File Name&quot; &amp; mn_LF _
  9. &amp; &quot;2. Record Tag Name&quot; &amp; mn_LF _
  10. &amp; &quot;3. A Range of Cells Containing Column Headers&quot; &amp; mn_LF _
  11. &amp; &quot;4. A Range of Cells Containing the Data Table.&quot; &amp; mn_LF _
  12. &amp; &quot;If You Are Ready To Proceed, Click &#39;Yes&#39;.&quot;, vbQuestion + vbYesNo, &quot;CreateXMLFile&quot;)
  13. If mn_YesOrNo = vbNo Then
  14. Debug.Print &quot;User Canceled With &#39;No&#39;&quot;
  15. Exit Sub
  16. End If
  17. mn_XML_FileName = GapFiller(InputBox(&quot;1. Enter the XML File Name:&quot;, &quot;CreateXMLFile&quot;, &quot;xml_file&quot;))
  18. If Right(mn_XML_FileName, 4) &lt;&gt; &quot;.xml&quot; Then
  19. mn_XML_FileName = mn_XML_FileName &amp; &quot;.xml&quot;
  20. End If
  21. mn_XML_Record_Name = GapFiller(InputBox(&quot;2. Enter The Record Tag Name:&quot;, &quot;CreateXMLFile&quot;, &quot;ID&quot;))
  22. mn_first_range = InputBox(&quot;3. Enter The Range of Cells Containing Column Headers:&quot;, &quot;CreateXMLFile&quot;, &quot;A1:B1&quot;)
  23. If MN_DataRange(mn_first_range, 1) &lt;&gt; MN_DataRange(mn_first_range, 2) Then
  24. MsgBox &quot;Error: Headers Must Be In The Same Row&quot; &amp; mn_LF &amp; &quot;Atcelts&quot;, vbOKOnly + vbCritical, &quot;CreateXMLFile&quot;
  25. Exit Sub
  26. End If
  27. MN_Row = MN_DataRange(mn_first_range, 1)
  28. For MN_Column = MN_DataRange(mn_first_range, 3) To MN_DataRange(mn_first_range, 4)
  29. If Len(Cells(MN_Row, MN_Column).Value) = 0 Then
  30. MsgBox &quot;Error: Header Contains Empty Cell&quot; &amp; mn_LF &amp; &quot;Canceled&quot;, vbOKOnly + vbCritical, &quot;CreateXMLFile&quot;
  31. Exit Sub
  32. End If
  33. mn_FieldName(MN_Column - MN_DataRange(mn_first_range, 3)) = GapFiller(Cells(MN_Row, MN_Column).Value)
  34. Next MN_Column
  35. mn_second_range = InputBox(&quot;4. Enter The Range of Cells Containing the Data Table:&quot;, &quot;CreateXMLFile&quot;, &quot;A2:B2&quot;)
  36. If MN_DataRange(mn_first_range, 4) - MN_DataRange(mn_first_range, 3) &lt;&gt; MN_DataRange(mn_second_range, 4) - MN_DataRange(mn_second_range, 3) Then
  37. MsgBox &quot;Error: There Are More Or Less Headers Than Columns of Data&quot; &amp; mn_LF &amp; &quot;Canceled&quot;, vbOKOnly + vbCritical, &quot;CreateXMLFile&quot;
  38. Exit Sub
  39. End If
  40. mn_rtc1 = MN_DataRange(mn_second_range, 3)
  41. If InStr(1, mn_XML_FileName, &quot;:\&quot;) = 0 Then
  42. mn_XML_FileName = mndefine_folder &amp; mn_XML_FileName
  43. End If
  44. Open mn_XML_FileName For Output As #1
  45. Print #1, &quot;&lt;?xml version=&quot; &amp; Chr(34) &amp; &quot;1.0&quot; &amp; Chr(34) &amp; &quot; encoding=&quot; &amp; Chr(34) &amp; &quot;ISO-8859-1&quot; &amp; Chr(34) &amp; &quot;?&gt;&quot;
  46. Print #1, &quot;&lt;DeclarationFile&gt;&quot;
  47. For MN_Row = MN_DataRange(mn_second_range, 1) To MN_DataRange(mn_second_range, 2)
  48. Print #1, &quot;&lt;&quot; &amp; mn_XML_Record_Name &amp; &quot;&gt;&quot;
  49. For MN_Column = mn_rtc1 To MN_DataRange(mn_second_range, 4)
  50. Print #1, &quot;&lt;&quot; &amp; mn_FieldName(MN_Column - mn_rtc1) &amp; &quot;&gt;&quot; &amp; AmpersandEliminate(CheckForm(MN_Row, MN_Column)) &amp; &quot;&lt;/&quot; &amp; mn_FieldName(MN_Column - mn_rtc1) &amp; &quot;&gt;&quot;
  51. Next MN_Column
  52. Print #1, &quot;&lt;/&quot; &amp; mn_XML_Record_Name &amp; &quot;&gt;&quot;
  53. Next MN_Row
  54. Print #1, &quot;&lt;/DeclarationFile&gt;&quot;
  55. Close #1
  56. MsgBox mn_XML_FileName &amp; &quot; izveidots.&quot; &amp; mn_LF &amp; &quot;Completed&quot;, vbOKOnly + vbInformation, &quot;CreateXMLFile&quot;
  57. Debug.Print mn_XML_FileName &amp; &quot; saved&quot;
  58. End Sub
  59. Function MN_DataRange(Rng_As_Text As String, MN_Item As Integer) As Integer
  60. Dim MN_user_range As Range
  61. Set MN_user_range = Range(Rng_As_Text)
  62. Select Case MN_Item
  63. Case 1
  64. MN_DataRange = MN_user_range.Row
  65. Case 2
  66. MN_DataRange = MN_user_range.Row + MN_user_range.Rows.Count - 1
  67. Case 3
  68. MN_DataRange = MN_user_range.Column
  69. Case 4
  70. MN_DataRange = MN_user_range.Columns(MN_user_range.Columns.Count).Column
  71. End Select
  72. Exit Function
  73. End Function
  74. Function GapFiller(mn_my_Str As String) As String
  75. Dim mn_Position As Integer
  76. mn_Position = InStr(1, mn_my_Str, &quot; &quot;)
  77. Do While mn_Position &gt; 0
  78. Mid(mn_my_Str, mn_Position, 1) = &quot;_&quot;
  79. mn_Position = InStr(1, mn_my_Str, &quot; &quot;)
  80. Loop
  81. GapFiller = LCase(mn_my_Str)
  82. End Function
  83. Function CheckForm(mn_Row_Number As Integer, mn_Column_Number As Integer) As String
  84. CheckForm = Cells(mn_Row_Number, mn_Column_Number).Value
  85. If IsNumeric(Cells(mn_Row_Number, mn_Column_Number).Value) Then
  86. CheckForm = Format(Cells(mn_Row_Number, mn_Column_Number).Value, &quot;#,##0 ;(#,##0)&quot;)
  87. End If
  88. If IsDate(Cells(mn_Row_Number, mn_Column_Number).Value) Then
  89. CheckForm = Format(Cells(mn_Row_Number, mn_Column_Number).Value, &quot;dd mmm yy&quot;)
  90. End If
  91. End Function
  92. Function AmpersandEliminate(mn_my_Str As String) As String
  93. Dim mn_Position As Integer
  94. mn_Position = InStr(1, mn_my_Str, &quot;&amp;&quot;)
  95. Do While mn_Position &gt; 0
  96. Mid(mn_my_Str, mn_Position, 1) = &quot;+&quot;
  97. mn_Position = InStr(1, mn_my_Str, &quot;&amp;&quot;)
  98. Loop
  99. AmpersandEliminate = mn_my_Str
  100. End Function

Since I am completely clueless with VBA, I was getting array errors and "argument not optional"

Any tips on where to start are appreciated!

答案1

得分: 0

请试试这个:

  1. Sub CreateXMLFile()
  2. Const THE_FOLDER As String = "C:\"
  3. Dim ws As Worksheet, rngData As Range, fName As String, rw As Long, col As Long
  4. Dim xml As String, tagId As String, tagVal As String, v
  5. If MsgBox("要尝试这个:" & vbLf & "1. XML文件名称" & vbLf & _
  6. "2. 包含数据表的单元格范围(包括标题)。" & vbLf & _
  7. "如果您准备好了,请单击 '是'", vbQuestion + vbYesNo, "CreateXMLFile") <> vbYes Then
  8. Debug.Print "用户取消了,选择 '否'"
  9. Exit Sub
  10. End If
  11. ' 最好在这里使用 `Application.GetSaveAsFileName`...
  12. fName = Application.GetSaveAsFilename(filefilter:="XML (*.xml),*.xml", _
  13. Title:="1. 选择保存XML文件的名称")
  14. On Error Resume Next ' 如果未选择范围,忽略错误
  15. Set rngData = Application.InputBox("2. 选择包含数据的范围(包括标题):", _
  16. "CreateXMLFile", Type:=8)
  17. On Error Resume Next ' 停止忽略错误
  18. If rngData Is Nothing Then
  19. Debug.Print "用户没有选择范围"
  20. Exit Sub
  21. End If
  22. Open fName For Output As #1
  23. Print #1, "<?xml version=""1.0"" encoding=""ISO-8859-1""?>"
  24. Print #1, "<DeclarationFile>"
  25. For rw = 2 To rngData.Rows.Count
  26. tagId = rngData.Cells(rw, 1).Value
  27. Print #1, "<" & tagId & ">"
  28. For col = 2 To rngData.Columns.Count
  29. tagVal = rngData.Cells(1, col).Value
  30. v = rngData.Cells(rw, col).Value
  31. Print #1, "<" & tagVal & ">" & Replace(CheckForm(v), "&", "+") & "</" & tagVal & ">"
  32. Next col
  33. Print #1, "</" & tagId & ">"
  34. Next rw
  35. Print #1, "</DeclarationFile>"
  36. Open fName For Output As #1
  37. Close #1
  38. MsgBox fName & " 创建。" & vbLf & "已完成", vbOKOnly + vbInformation, "CreateXMLFile"
  39. Debug.Print fName & " 已保存"
  40. End Sub
  41. Function CheckForm(v) As String
  42. If IsNumeric(v) Then v = Format(v, "#,##0 ;(#,##0)")
  43. If IsDate(v) Then v = Format(v, "dd mmm yy")
  44. CheckForm = CStr(v)
  45. End Function
英文:

Try this:

  1. Sub CreateXMLFile()
  2. Const THE_FOLDER As String = &quot;C:\&quot;
  3. Dim ws As Worksheet, rngData As Range, fName As String, rw As Long, col As Long
  4. Dim xml As String, tagId As String, tagVal As String, v
  5. If MsgBox(&quot;Vajadzigs:&quot; &amp; vbLf &amp; &quot;1. XML File Name&quot; &amp; vbLf &amp; _
  6. &quot;2. A Range of Cells Containing the Data Table (with headers).&quot; &amp; vbLf &amp; _
  7. &quot;If You Are Ready To Proceed, Click &#39;Yes&#39;.&quot;, vbQuestion + vbYesNo, &quot;CreateXMLFile&quot;) &lt;&gt; vbYes Then
  8. Debug.Print &quot;User Canceled With &#39;No&#39;&quot;
  9. Exit Sub
  10. End If
  11. &#39;would be better to use `Application.GetSaveAsFileName` here...
  12. fName = Application.GetSaveAsFilename(filefilter:=&quot;XML (*.xml),*.xml&quot;, _
  13. Title:=&quot;1. Select SaveAs name for XML file&quot;)
  14. On Error Resume Next &#39;ignore error if not range selected
  15. Set rngData = Application.InputBox(&quot;2. Select the range with data (include headers):&quot;, _
  16. &quot;CreateXMLFile&quot;, Type:=8)
  17. On Error Resume Next &#39;stop ignoring error
  18. If rngData Is Nothing Then
  19. Debug.Print &quot;User did not select a range&quot;
  20. Exit Sub
  21. End If
  22. Open fName For Output As #1
  23. Print #1, &quot;&lt;?xml version=&quot;&quot;1.0&quot;&quot; encoding=&quot;&quot;ISO-8859-1&quot;&quot;?&gt;&quot;
  24. Print #1, &quot;&lt;DeclarationFile&gt;&quot;
  25. For rw = 2 To rngData.Rows.Count
  26. tagId = rngData.Cells(rw, 1).Value
  27. Print #1, &quot;&lt;&quot; &amp; tagId &amp; &quot;&gt;&quot;
  28. For col = 2 To rngData.Columns.Count
  29. tagVal = rngData.Cells(1, col).Value
  30. v = rngData.Cells(rw, col).Value
  31. Print #1, &quot;&lt;&quot; &amp; tagVal &amp; &quot;&gt;&quot; &amp; Replace(CheckForm(v), &quot;&amp;&quot;, &quot;+&quot;) &amp; &quot;&lt;/&quot; &amp; tagVal &amp; &quot;&gt;&quot;
  32. Next col
  33. Print #1, &quot;&lt;/&quot; &amp; tagId &amp; &quot;&gt;&quot;
  34. Next rw
  35. Print #1, &quot;&lt;/DeclarationFile&gt;&quot;
  36. Open fName For Output As #1
  37. Close #1
  38. MsgBox fName &amp; &quot; izveidots.&quot; &amp; vbLf &amp; &quot;Completed&quot;, vbOKOnly + vbInformation, &quot;CreateXMLFile&quot;
  39. Debug.Print fName &amp; &quot; saved&quot;
  40. End Sub
  41. Function CheckForm(v) As String
  42. If IsNumeric(v) Then v = Format(v, &quot;#,##0 ;(#,##0)&quot;)
  43. If IsDate(v) Then v = Format(v, &quot;dd mmm yy&quot;)
  44. CheckForm = CStr(v)
  45. End Function

huangapple
  • 本文由 发表于 2023年2月16日 02:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定