Anzeige
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Anzeige
Inhaltsverzeichnis

leere Zellen bei XML-Export nicht uebernehmen

Forumthread: leere Zellen bei XML-Export nicht uebernehmen

leere Zellen bei XML-Export nicht uebernehmen
02.04.2017 14:57:25
Enrico
Hi.
ich moechte per VBA aus einer Tabelle eine XML-Datei erstellen.
In meiner Tabelle gibt es unter anderem 4 Spalten "int","string", "boolean" und "double".
Pro Datensatz steht immer nur in einer dieser 4 Spalten ein Wert.
Mein Code bisher sieht folgendermassen aus:
<pre>Sub XML_Export()
Dim Datei As String, Text As String
Dim lngRow, lngCol As Long
Dim varShow
Datei = ThisWorkbook.Path & "\Coordtable.xml"
Open Datei For Output As #1
Print #1, "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> "
Print #1, "<NamedTypedObjectCollection xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">"
For lngRow = 2 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Print #1, "<NamedTypedObject Name=""" & Cells(lngRow, 1) & """>"
Print #1, "<Type>" & Cells(lngRow, 2) & "</Type>"
Print #1, "<UserInput Name=""" & Cells(lngRow, 3) & """>"
Print #1, "<Expressionable>" & Cells(lngRow, 4) & "</Expressionable>"
Print #1, "<Category>" & Cells(lngRow, 5) & "</Category>"
Print #1, "<ReadOnly>" & Cells(lngRow, 6) & "</ReadOnly>"
Print #1, "<PropertyType>" & Cells(lngRow, 7) & "</PropertyType>"
Print #1, "<DefaultValue>" & Cells(lngRow, 8) & "</DefaultValue>"
Print #1, "<int>" & Cells(lngRow, 9) & "</int>"
Print #1, "<string>" & Cells(lngRow, 10) & "</string>"
Print #1, "<boolean>" & Cells(lngRow, 11) & "</boolean>"
Print #1, "<boolean>" & Cells(lngRow, 12) & "</double>"
Print #1, "</UserInput>"
Print #1, "</NamedTypedObject>"
Next lngRow
Print #1, "</daten>"
Close #1
End Sub</pre>
~f~
Bisher nimmt er alle Spalten mit - logisch, aber gibt es eine Moeglichkeit, dass nur die Zelle geschrieben wird, wenn sie auch ein Inhalt hat?
~f~
Print #1, "<int>" & Cells(lngRow, 9) & "</int>"
Print #1, "<string>" & Cells(lngRow, 10) & "</string>"
Print #1, "<boolean>" & Cells(lngRow, 11) & "</boolean>"
Print #1, "<boolean>" & Cells(lngRow, 12) & "</double>"
Danke und viele Gruesse
Rico
Anzeige

4
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: leere Zellen bei XML-Export nicht uebernehmen
03.04.2017 10:57:08
UweD
Hallo
ungetestet...
Option Explicit 
 
Sub XML_Export() 
 
    Dim Datei As String, Text As String 
    Dim lngRow, lngCol As Long 
    Dim varShow 
     
    Datei = ThisWorkbook.Path & "\Coordtable.xml" 
     
    Open Datei For Output As #1 
     
    Print #1, "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> " 
    Print #1, "<NamedTypedObjectCollection xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">" 
     
    For lngRow = 2 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row 
     
        Print #1, "<NamedTypedObject Name=""" & Cells(lngRow, 1) & """>" 
        Print #1, "<Type>" & Cells(lngRow, 2) & "</Type>" 
         
        Print #1, "<UserInput Name=""" & Cells(lngRow, 3) & """>" 
        Print #1, "<Expressionable>" & Cells(lngRow, 4) & "</Expressionable>" 
        Print #1, "<Category>" & Cells(lngRow, 5) & "</Category>" 
        Print #1, "<ReadOnly>" & Cells(lngRow, 6) & "</ReadOnly>" 
        Print #1, "<PropertyType>" & Cells(lngRow, 7) & "</PropertyType>" 
        Print #1, "<DefaultValue>" & Cells(lngRow, 8) & "</DefaultValue>" 
        Print #1, "<int>" & Cells(lngRow, 9) & "</int>" 
        If Wert_Da(10) Then Print #1, "<string>" & Cells(lngRow, 10) & "</string>" 
        If Wert_Da(11) Then Print #1, "<boolean>" & Cells(lngRow, 11) & "</boolean>" 
        If Wert_Da(12) Then Print #1, "<boolean>" & Cells(lngRow, 12) & "</double>" 
         
        Print #1, "</UserInput>" 
         
        Print #1, "</NamedTypedObject>" 
         
    Next lngRow 
     
    Print #1, "</daten>" 
     
    Close #1 
 
End Sub 
 
Function Wert_Da(Zeile) As Boolean 
    Wert_Da = WorksheetFunction.CountA(Rows(Zeile)) > 0 
End Function 
 

LG UweD
Anzeige
AW: leere Zellen bei XML-Export nicht uebernehmen
03.04.2017 12:29:47
Enrico
Hi Uwe,
hat leider nicht geklappt, trotzdem vielen Dank! Ich habe es zwischenzeitlich so gelöst:
Print #1, "<DefaultValue>" & Cells(lngRow, 8) & "</DefaultValue>"
If Cells(lngRow, 9) <> "" Then
Print #1, "<int>" & Cells(lngRow, 9) & "</int>"
End If
If Cells(lngRow, 10) <> "" Then
Print #1, "<string>" & Cells(lngRow, 10) & "</string>"
End If
If Cells(lngRow, 11) <> "" Then
Print #1, "<boolean>" & Cells(lngRow, 11) & "</boolean>"
End If
If Cells(lngRow, 12) <> "" Then
Print #1, "<double>" & Cells(lngRow, 12) & "</double>"
End If
Print #1, "</UserInput>"
LG Rico
Anzeige
AW: leere Zellen bei XML-Export nicht uebernehmen
03.04.2017 12:43:29
UweD
Ok.
Dann hast du sicher Formeln in den Zeilen die "" liefern. Oder?
LG UweD
AW: leere Zellen bei XML-Export nicht uebernehmen
03.04.2017 12:45:56
Enrico
Ja stimmt ... aber um ehrlich zu sein, wahr mir das nicht bewusst, dass da ein Zusammenhang besteht.

Forumthreads zu verwandten Themen

Anzeige
Anzeige