各ユニークなデータ要素の下に反復する属性タグのためにExcelからXMLファイルを生成する必要があります。 以下はExcel形式です。 Data要素の一意性は、XMLファイルには表示されないID列によって識別されます。各固有の要素の下に繰り返しタグとしてExcelからXMLを生成
Excelのフォーマット:(入力)
ID | Name | Description | AttributeName | AttributeValue
--------------------------------------------------------
01 | A | Test1 | Width | 33
| | | Height | 50
| | | Length | 25
02 | B | Test2 | Width | 55
| | | Depth | 88
XMLフォーマット:(予想される出力)
<Data name="A" description="Test1">
<Attribute Name="Width" Value="33"/>
<Attribute Name="Heigth" Value="50"/>
<Attribute Name="Length" Value="25"/>
</Data>
<Data name="B" description="Test2">
<Attribute Name="Width" Value="55"/>
<Attribute Name="Depth" Value="88"/>
</Data>
</List>
これは私がコードにしようとしているものです。
VBAコード:
Sub GenerateXML_Test()
Dim myFile As String
'Create XML File
myFile = ThisWorkbook.Path + "\TestXML.xml"
Open myFile For Output As #1
'Get Last Row
LastRow = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
Print #1, "<List>" & vbNewLine
'Print recurring text in xml file
For RowIndex = 2 To LastRow
Print #1, "<Data name=" & Chr(34) & Cells(RowIndex, 2).Value & Chr(34) & " Description=" & Chr(34) & Cells(RowIndex, 3).Value & Chr(34) & ">" & vbNewLine & "<Attribute Name=" & Chr(34) & Cells(RowIndex, 4).Value & Chr(34) & " Value=" & Chr(34) & Cells(RowIndex, 5).Value & Chr(34) & "/>" & "</Data>" & vbNewLine
Next RowIndex
Print #1, "</List>"
Close #1
End Sub
XMLフォーマット:(実際の出力)
<List>
<Data name="A" Description="Test1">
<Attribute Name="Width" Value="33" />
</Data>
<Data name="" Description="">
<Attribute Name="Height" Value="50" />
</Data>
<Data name="" Description="">
<Attribute Name="Length" Value="25" />
</Data>
<Data name="B" Description="Test2">
<Attribute Name="Width" Value="55" />
</Data>
<Data name="" Description="">
<Attribute Name="Depth" Value="88" />
</Data>
</List>
出力Iは、適切なロジックを追加する必要があるとして、期待通りではありません。属性タグは、一意のIDごとに単一のデータ要素で繰り返す必要があります。各データ要素には、任意の数の属性を設定できます。 これを達成するロジックを追加するのを手伝ってください。
コードは正常に動作します。ありがとうございました。 – Renee