Excel - bedingte Formatierung; Probleme mit der Hintergrundfarbzuweisung

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von petaod.

    Excel - bedingte Formatierung; Probleme mit der Hintergrundfarbzuweisung

    Moin!

    ich habe eine Verbindung zu Excel erstellt und möchte bedingte Formatierungen für den Hintergrund definieren.

    Hier mein Code - in der Hoffnung nichts vergessen zu haben bei den Ausschnitten.

    VB.NET-Quellcode

    1. Imports Microsoft.Office.Interop
    2. Imports xls = Microsoft.Office.Interop.Excel
    3. ...
    4. Private Sub EBL_Insp_Crawler_Work(Laufwerk As String)
    5. Const k_Text_undef As String = "./."
    6. Const k_Text_OK As String = "OK"
    7. Const k_Text_ng As String = "n.g."
    8. Const k_Text_Err As String = "Fehler!"
    9. Const k_Color_Green As Integer = 5296274
    10. Const k_Color_Red As Integer = 255
    11. Const k_Color_Yellow As Integer = 65535
    12. Const k_Color_Blue As Integer = 15773696
    13. ' öffnen der Excel-Datei
    14. Dim xls_Appl As Excel.Application
    15. Dim xls_Mappe As Excel.Workbook ' Excel Arbeitsmappe
    16. Dim xls_Blatt As Excel.Worksheet ' Excel Blatt ( Tabelle)
    17. xls_Appl = CType(CreateObject("Excel.Application"), Excel.Application)
    18. xls_Appl.Visible = False
    19. xls_Mappe = xls_Appl.Workbooks.Add 'Hier kommt kein gültiges Objekt zurück
    20. 'xls_Mappe = xls_Appl.Workbooks.Open(FullPathStundenZettel)
    21. With xls_Blatt
    22. Const k_Verz_XML As Integer = 2
    23. BedFormatBackground(xls_Blatt, k_Verz_XML, "RED", k_Color_Red)
    24. BedFormatBackground(xls_Blatt, k_Verz_XML, "GREEN", k_Color_Green)
    25. BedFormatBackground(xls_Blatt, k_Verz_XML, "BLUE", k_Color_Blue)
    26. end with
    27. End Sub
    28. Private Sub BedFormatBackground(ByRef sheet As Excel.Worksheet, iSpalte As Integer, sValue As String, Color As Integer)
    29. Dim ColLetter As String = _XLS_Write.Ziffer2ExcelColumn(iSpalte)
    30. Dim r As Object
    31. Try
    32. r = sheet.Range(ColLetter & ":" & ColLetter).Select()
    33. With sheet.Range(ColLetter & ":" & ColLetter)
    34. .FormatConditions.Add(Type:=Microsoft.Office.Interop.Excel.XlFormatConditionType.xlTextString, String:=sValue, TextOperator:=2)
    35. .FormatConditions(.FormatConditions.Count).SetFirstPriority()
    36. .FormatConditions(.FormatConditions.Count).Interior.PatternColorIndex = Microsoft.Office.Interop.Excel.Constants.xlAutomatic
    37. .FormatConditions(.FormatConditions.Count).Interior.ThemeColor = Microsoft.Office.Interop.Excel.XlThemeColor.xlThemeColorAccent3
    38. .FormatConditions(.FormatConditions.Count).Interior.TintAndShade = 0.0
    39. End With
    40. Catch ex As Exception
    41. Dim TryReport As New EBL.Service.TryCatchReport
    42. TryReport.Show("unerwarteter Fehler in EBL.Verwaltung > INSP_Crawler > BedFormatBackground" & vbCrLf & ex.ToString)
    43. End Try
    44. End Sub


    Das Problem ist nun, dass immer nur eine Farbe im Regelwerk auftaucht und der Rest nur weiß ist.



    Kann mir einer von Euch weiterhelfen ?

    Gruß Jan
    Hallo Jan
    Ich habe keine Ahnung was Excel diesmal durcheinander bringt.
    Aber nach dem umgestalten des With-Blockes, funktioniert es nun.
    Ausserdem habe ich "ThemeColor" entfernt , dafür "Color" und "StopIfTrue" hinzugefügt.

    VB.NET-Quellcode

    1. Private Sub BedFormatBackground(ByRef sheet As Excel.Worksheet, iSpalte As Integer, sValue As String, Color As Integer)
    2. Dim ColLetter As String = _XLS_Write.Ziffer2ExcelColumn(iSpalte)
    3. Dim r As Excel.Range
    4. Dim fc As Excel.FormatCondition
    5. Try
    6. r = sheet.Range(ColLetter & ":" & ColLetter)
    7. fc = CType(r.FormatConditions.Add(Type:=Excel.XlFormatConditionType.xlTextString, String:=sValue, _
    8. TextOperator:=Excel.XlContainsOperator.xlBeginsWith), Excel.FormatCondition)
    9. With fc
    10. .SetFirstPriority()
    11. .Interior.PatternColorIndex = Excel.Constants.xlAutomatic
    12. .Interior.Color = Color
    13. '.Interior.ThemeColor = Excel.XlThemeColor.xlThemeColorAccent3
    14. .Interior.TintAndShade = 0.0
    15. .StopIfTrue = True
    16. End With
    17. Catch ex As Exception
    18. Dim TryReport As New EBL.Service.TryCatchReport
    19. TryReport.Show("unerwarteter Fehler in EBL.Verwaltung > INSP_Crawler > BedFormatBackground" & vbCrLf & ex.ToString)
    20. End Try
    21. End Sub
    Gruss HenryV

    HenryV schrieb:

    nach dem umgestalten des With-Blockes, funktioniert es nun.
    Eigentlich logisch.

    jan99 schrieb:

    .FormatConditions(.FormatConditions.Count).SetFirstPriority()
    .FormatConditions(.FormatConditions.Count).Interior.PatternColorIndex = Microsoft.Office.Interop.Excel.Constants.xlAutomatic
    Das erste Statement schiebt die Formatbedingung auf den ersten Platz.
    Das zweite Statement verändert die Formatbedingung auf dem letzten Platz.

    Das Zuweisen an eine Objektvariable ist der saubere Weg (so wie von @HenryV gezeigt.
    Dann weiß man wenigstens, welche Formatbedingung gemeint ist.
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --