Zeichen ändern

  • Excel

Es gibt 12 Antworten in diesem Thema. Der letzte Beitrag () ist von asdfmilchkuh.

    Zeichen ändern

    Hallo Leute,

    ich komme nicht weiter und ich hoffe, dass mir hier weitergeholfen werden kann.

    Ich habe einen VBA-Code erstellt, der die Excel-Datei als XML "umschreibt", hier der Code:

    Spoiler anzeigen

    Visual Basic-Quellcode

    1. Sub XML_Export()
    2. Dim Datei As String, Text As String
    3. Dim Zeile As Long
    4. Dim alt As String
    5. Dim neu As String
    6. On Error GoTo Hell
    7. 'Speichern
    8. Datei = ThisWorkbook.Path & "\KPI.xml"
    9. Open Datei For Output As #1
    10. 'reinschreiben
    11. Print #1, "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> "
    12. Print #1, "<dataroot>"
    13. 'Schleife
    14. For Zeile = 2 To 23
    15. Print #1, "<tblFirma>"
    16. Print #1, "" & vbTab & "<FName>" & Cells(Zeile, 1) & "</FName>"
    17. Print #1, "" & vbTab & "<Headquater>" & Cells(Zeile, 2) & "</Headquater>"
    18. Print #1, "" & vbTab & "<Turnover>" & Cells(Zeile, 3) & "</Turnover>"
    19. Print #1, "" & vbTab & "<Markets>" & Cells(Zeile, 4) & "</Markets>"
    20. Print #1, "" & vbTab & "<Locations>" & Cells(Zeile, 5) & "</Locations>"
    21. Print #1, "" & vbTab & "<FShare>" & Cells(Zeile, 6) & "</FShare>"
    22. Print #1, "" & vbTab & "<qmtotal>" & Cells(Zeile, 7) & "</qmtotal>"
    23. Print #1, "" & vbTab & "<qmpOutlet>" & Cells(Zeile, 8) & "</qmpOutlet>"
    24. Print #1, "" & vbTab & "<Employees>" & Cells(Zeile, 9) & "</Employees>"
    25. Print #1, "" & vbTab & "<Formats>" & Cells(Zeile, 10) & "</Formats>"
    26. Print #1, "" & vbTab & "<DC>" & Cells(Zeile, 11) & "</DC>"
    27. Print #1, "" & vbTab & "<SKU>" & Cells(Zeile, 12) & "</SKU>"
    28. Print #1, "" & vbTab & "<ERP>" & Cells(Zeile, 13) & "</ERP>"
    29. Print #1, "" & vbTab & "<Logistic>" & Cells(Zeile, 14) & "</Logistic>"
    30. Print #1, "" & vbTab & "<CRM>" & Cells(Zeile, 15) & "</CRM>"
    31. Print #1, "" & vbTab & "<Merch>" & Cells(Zeile, 16) & "</Merch>"
    32. Print #1, "" & vbTab & "<POS>" & Cells(Zeile, 17) & "</POS>"
    33. Print #1, "" & vbTab & "<Webshop>" & Cells(Zeile, 18) & "</Webshop>"
    34. Print #1, "" & vbTab & "<PIM>" & Cells(Zeile, 19) & "</PIM>"
    35. Print #1, "" & vbTab & "<MDM>" & Cells(Zeile, 20) & "</MDM>"
    36. Print #1, "" & vbTab & "<EBIT>" & Cells(Zeile, 21) & "</EBIT>"
    37. Print #1, "" & vbTab & "<Profit>" & Cells(Zeile, 22) & "</Profit>"
    38. Print #1, "" & vbTab & "<Revenue>" & Cells(Zeile, 23) & "</Revenue>"
    39. Print #1, "" & vbTab & "<CEO>" & Cells(Zeile, 24) & "</CEO>"
    40. Print #1, "" & vbTab & "<CFO>" & Cells(Zeile, 25) & "</CFO>"
    41. Print #1, "" & vbTab & "<CIO>" & Cells(Zeile, 26) & "</CIO>"
    42. Print #1, "</tblFirma>"
    43. Next Zeile
    44. Print #1, "</dataroot>"
    45. Close #1
    46. Exit Sub
    47. Hell:
    48. Close #1
    49. MsgBox "FehlerNr.: " & Err.Number & vbNewLine & vbNewLine _
    50. & "Beschreibung: " & Err.Description _
    51. , vbCritical, "Fehler"
    52. End Sub


    Das Problem ist, dass ich bei FName manchmal das "&" benutze, was meine XML Datei zerschießt. Meine Frage, wie kriege ich es hin, dass das "&" zu "&amp;" wird?

    Habe es mit

    Visual Basic-Quellcode

    1. Dim alt As String
    2. Dim neu As String
    3. alt = "&"
    4. neu = Replace(alt, "&", "&amp;")


    probiert, leider ohne Erfolg.

    Ich bedanke mich schon einmal im Voraus!

    Gruß
    Wie soll das auch funktionieren? Du musst dem Relpace schon mitgeben aus welchem Text er das Zeichen ersetzen soll.
    Versuchs mal so:

    VB.NET-Quellcode

    1. Dim alt As String
    2. Dim neu As String
    3. alt = "&"
    4. neu = Replace(alt,"&","&amp;")
    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."
    Suchen uns ersetzen im Notepad:

    Spoiler anzeigen

    Quellcode

    1. Sub XML_Export()
    2. Dim Datei As String, Text As String
    3. Dim Zeile As Long
    4. Dim alt As String
    5. Dim neu As String
    6. On Error GoTo Hell
    7. 'Speichern
    8. Datei = ThisWorkbook.Path &amp; "\KPI.xml"
    9. Open Datei For Output As #1
    10. 'reinschreiben
    11. Print #1, "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> "
    12. Print #1, "<dataroot>"
    13. 'Schleife
    14. For Zeile = 2 To 23
    15. Print #1, "<tblFirma>"
    16. Print #1, "" &amp; vbTab &amp; "<FName>" &amp; Cells(Zeile, 1) &amp; "</FName>"
    17. Print #1, "" &amp; vbTab &amp; "<Headquater>" &amp; Cells(Zeile, 2) &amp; "</Headquater>"
    18. Print #1, "" &amp; vbTab &amp; "<Turnover>" &amp; Cells(Zeile, 3) &amp; "</Turnover>"
    19. Print #1, "" &amp; vbTab &amp; "<Markets>" &amp; Cells(Zeile, 4) &amp; "</Markets>"
    20. Print #1, "" &amp; vbTab &amp; "<Locations>" &amp; Cells(Zeile, 5) &amp; "</Locations>"
    21. Print #1, "" &amp; vbTab &amp; "<FShare>" &amp; Cells(Zeile, 6) &amp; "</FShare>"
    22. Print #1, "" &amp; vbTab &amp; "<qmtotal>" &amp; Cells(Zeile, 7) &amp; "</qmtotal>"
    23. Print #1, "" &amp; vbTab &amp; "<qmpOutlet>" &amp; Cells(Zeile, 8) &amp; "</qmpOutlet>"
    24. Print #1, "" &amp; vbTab &amp; "<Employees>" &amp; Cells(Zeile, 9) &amp; "</Employees>"
    25. Print #1, "" &amp; vbTab &amp; "<Formats>" &amp; Cells(Zeile, 10) &amp; "</Formats>"
    26. Print #1, "" &amp; vbTab &amp; "<DC>" &amp; Cells(Zeile, 11) &amp; "</DC>"
    27. Print #1, "" &amp; vbTab &amp; "<SKU>" &amp; Cells(Zeile, 12) &amp; "</SKU>"
    28. Print #1, "" &amp; vbTab &amp; "<ERP>" &amp; Cells(Zeile, 13) &amp; "</ERP>"
    29. Print #1, "" &amp; vbTab &amp; "<Logistic>" &amp; Cells(Zeile, 14) &amp; "</Logistic>"
    30. Print #1, "" &amp; vbTab &amp; "<CRM>" &amp; Cells(Zeile, 15) &amp; "</CRM>"
    31. Print #1, "" &amp; vbTab &amp; "<Merch>" &amp; Cells(Zeile, 16) &amp; "</Merch>"
    32. Print #1, "" &amp; vbTab &amp; "<POS>" &amp; Cells(Zeile, 17) &amp; "</POS>"
    33. Print #1, "" &amp; vbTab &amp; "<Webshop>" &amp; Cells(Zeile, 18) &amp; "</Webshop>"
    34. Print #1, "" &amp; vbTab &amp; "<PIM>" &amp; Cells(Zeile, 19) &amp; "</PIM>"
    35. Print #1, "" &amp; vbTab &amp; "<MDM>" &amp; Cells(Zeile, 20) &amp; "</MDM>"
    36. Print #1, "" &amp; vbTab &amp; "<EBIT>" &amp; Cells(Zeile, 21) &amp; "</EBIT>"
    37. Print #1, "" &amp; vbTab &amp; "<Profit>" &amp; Cells(Zeile, 22) &amp; "</Profit>"
    38. Print #1, "" &amp; vbTab &amp; "<Revenue>" &amp; Cells(Zeile, 23) &amp; "</Revenue>"
    39. Print #1, "" &amp; vbTab &amp; "<CEO>" &amp; Cells(Zeile, 24) &amp; "</CEO>"
    40. Print #1, "" &amp; vbTab &amp; "<CFO>" &amp; Cells(Zeile, 25) &amp; "</CFO>"
    41. Print #1, "" &amp; vbTab &amp; "<CIO>" &amp; Cells(Zeile, 26) &amp; "</CIO>"
    42. Print #1, "</tblFirma>"
    43. Next Zeile
    44. Print #1, "</dataroot>"
    45. Close #1
    46. Exit Sub
    47. Hell:
    48. Close #1
    49. MsgBox "FehlerNr.: " &amp; Err.Number &amp; vbNewLine &amp; vbNewLine _
    50. &amp; "Beschreibung: " &amp; Err.Description _
    51. , vbCritical, "Fehler"
    52. End Sub

    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."
    Ja, das wäre mir schon klar. Nur das Problem ist, dass mehrere Leute diese Datei nutzen und zu 90% ist für diese Personen Notepad, Sublime, Word etc. + "suchen und ersetzen" ein Fremdwort ist. Deswegen wollte ich das mit vba umgehen.

    Den Code, den du gepostet hast, ist der gleiche wie meiner, richtig?
    jupp.

    Der Code

    VB.NET-Quellcode

    1. Dim alt As String
    2. Dim neu As String
    3. alt = "&"
    4. neu = Replace(alt,"&","&amp;")


    in deinem Code wäre das dann in zeile 22 so aus:

    VB.NET-Quellcode

    1. strTest = Replace(Cells(Zeile, 1), "&", "&amp;")
    2. Print #1, "" & vbTab & "<FName>" & strTest & "</FName>"


    Macht genau das was du willst. Setz mal einen Haltepunkt auf "neu = Replace(...)" und schau dir den Text in "alt" an. Vielleicht steht ja was völlig anderes darin wie du vermutest
    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Schamash“ ()

    Wenn das "&" in den Cells steht, dann so:

    Visual Basic-Quellcode

    1. dim tmp as string
    2. ...
    3. ...
    4. ...
    5. For Zeile = 2 To 23
    6. Print #1, "<tblFirma>"
    7. tmp = replace(Cells(Zeile, 1), "&", "&amp;")
    8. Print #1, "" & vbTab & "<FName>" & tmp & "</FName>"
    9. tmp = replace(Cells(Zeile, 2), "&", "&amp;")
    10. Print #1, "" & vbTab & "<Headquater>" & tmp & "</Headquater>"
    11. usw.


    Besser und schneller geht's so:

    Visual Basic-Quellcode

    1. For Zeile = 2 To 23
    2. Print #1, "<tblFirma>"
    3. tmp = replace(Cells(Zeile, 1), "&", "&amp;")
    4. Print #1, ""; vbTab; "<FName>"; tmp; "</FName>"
    5. tmp = replace(Cells(Zeile, 2), "&", "&amp;")
    6. Print #1, ""; vbTab; "<Headquater>"; tmp; "</Headquater>"


    Dann braucht der Ausgabestring nicht erst zusammengesetzt werden.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Eierlein“ ()

    There is no CLOUD - just other people's computers

    Q: Why do JAVA developers wear glasses?
    A: Because they can't C#

    Daily prayer:
    "Dear Lord, grand me the strength not to kill any stupid people today and please grant me the ability to punch them in the face over standard TCP/IP."
    da beide Betriebssysteme (Windows und OSX) benutzt werden

    Die Beispiele funktioniern nur unter Windows. Wie bei IOS nach UTF8 konvertiert werden kann:Keine Ahnung.

    Wenn es sich hauptsächlich um Umlaute handelt, kann man es auch selbst 'Replacen':
    Spoiler anzeigen

    Visual Basic-Quellcode

    1. ...
    2. ...
    3. Dim tmp as string
    4. ' Ausgabe String zusammensetzen
    5. tmp = "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> " & vbcrlf & _
    6. "<dataroot>" & vbcrlf
    7. 'Schleife
    8. For Zeile = 2 To 23
    9. tmp = tmp & "<tblFirma>" & vbcrlf & _
    10. vbTab & "<FName>" & Cells(Zeile, 1) & "</FName>" & vbcrlf & _
    11. vbTab & "<Headquater>" & Cells(Zeile, 2) & "</Headquater>" & vbcrlf & _
    12. vbTab & "<Turnover>" & Cells(Zeile, 3) & "</Turnover>" & vbcrlf & _
    13. vbTab & "<Markets>" & Cells(Zeile, 4) & "</Markets>" & vbcrlf & _
    14. vbTab & "<Locations>" & Cells(Zeile, 5) & "</Locations>" & vbcrlf & _
    15. vbTab & "<FShare>" & Cells(Zeile, 6) & "</FShare>" & vbcrlf & _
    16. vbTab & "<qmtotal>" & Cells(Zeile, 7) & "</qmtotal>" & vbcrlf & _
    17. vbTab & "<qmpOutlet>" & Cells(Zeile, 8) & "</qmpOutlet>" & vbcrlf & _
    18. vbTab & "<Employees>" & Cells(Zeile, 9) & "</Employees>" & vbcrlf & _
    19. vbTab & "<Formats>" & Cells(Zeile, 10) & "</Formats>" & vbcrlf & _
    20. vbTab & "<DC>" & Cells(Zeile, 11) & "</DC>" & vbcrlf & _
    21. vbTab & "<SKU>" & Cells(Zeile, 12) & "</SKU>" & vbcrlf & _
    22. vbTab & "<ERP>" & Cells(Zeile, 13) & "</ERP>" & vbcrlf & _
    23. vbTab & "<Logistic>" & Cells(Zeile, 14) & "</Logistic>" & vbcrlf & _
    24. vbTab & "<CRM>" & Cells(Zeile, 15) & "</CRM>" & vbcrlf & _
    25. vbTab & "<Merch>" & Cells(Zeile, 16) & "</Merch>" & vbcrlf & _
    26. vbTab & "<POS>" & Cells(Zeile, 17) & "</POS>" & vbcrlf & _
    27. vbTab & "<Webshop>" & Cells(Zeile, 18) & "</Webshop>" & vbcrlf & _
    28. vbTab & "<PIM>" & Cells(Zeile, 19) & "</PIM>" & vbcrlf & _
    29. vbTab & "<MDM>" & Cells(Zeile, 20) & "</MDM>" & vbcrlf & _
    30. vbTab & "<EBIT>" & Cells(Zeile, 21) & "</EBIT>" & vbcrlf & _
    31. vbTab & "<Profit>" & Cells(Zeile, 22) & "</Profit>" & vbcrlf & _
    32. vbTab & "<Revenue>" & Cells(Zeile, 23) & "</Revenue>" & vbcrlf & _
    33. vbTab & "<CEO>" & Cells(Zeile, 24) & "</CEO>" & vbcrlf & _
    34. vbTab & "<CFO>" & Cells(Zeile, 25) & "</CFO>" & vbcrlf & _
    35. vbTab & "<CIO>" & Cells(Zeile, 26) & "</CIO>" & vbcrlf & _
    36. "</tblFirma>" & vbcrlf
    37. Next Zeile
    38. tmp = tmp & "</dataroot>"
    39. tmp = replace(tmp, "&", "&amp;")
    40. ' konvertieren in UTF8
    41. tmp = replace(tmp, "ä", "ä")
    42. tmp = replace(tmp, "ö", "ö")
    43. tmp = replace(tmp, "ü", "ü")
    44. tmp = replace(tmp, "Ä", "Ä")
    45. tmp = replace(tmp, "Ö", "Ö")
    46. tmp = replace(tmp, "Ü", "Ãœ")
    47. tmp = replace(tmp, "ß", "ß")
    48. tmp = replace(tmp, "€", "€")
    49. 'usw.
    50. 'tmp = replace(tmp,
    51. 'Speichern
    52. Datei = ThisWorkbook.Path & "\KPI.xml"
    53. Open Datei For Output As #1
    54. 'Print #1, "" ' Nur falls der UTF8-BOM benötigt wird
    55. Print #1, tmp
    56. close
    57. ...
    58. ...


    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Eierlein“ ()