Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1912to1916
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Bedingtform. ListObjects -> Spaltenname

Bedingtform. ListObjects -> Spaltenname
04.01.2023 08:50:26
Mathias
Hallo an alle
Es wäre sehr nett wenn mir jemand weiterhelfen könnte. Ich möchte bedinget Formatierungen über VBA setzen das funktioniert auch gut der CodE wird bei jedem speichern ausgeführt aber.
Ist es möglich über den Spalten Namen (also die Überschrift wie zbsp Datum) einer Tabelle(ListObjects) bedingt zu formatieren. Im Moment mache ich es über die Nummer der Spalte. Nur leider werden immer wieder Spalten in ihrer Position innerhalb der Tabelle durch meinen Chef aus Bequemlichkeit verschoben oder Leerzeilen über der Tabelle eingefügt. Ich darf die Tabelle allerdings nicht Sperren.
so mache ich es im derzeit:

   ActiveSheet.Cells.FormatConditions.Delete
With wsAktiv.ListObjects(1).ListColumns(12)
.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ISTLEER($L2)=WAHR"
.DataBodyRange.FormatConditions(.DataBodyRange.FormatConditions.Count).SetFirstPriority
With .DataBodyRange.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0.799981688894314
End With
.DataBodyRange.FormatConditions(1).StopIfTrue = False
End With
With wsAktiv.ListObjects(1).ListColumns(13)
.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ISTLEER($M2)=WAHR"
.DataBodyRange.FormatConditions(.DataBodyRange.FormatConditions.Count).SetFirstPriority
With .DataBodyRange.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0.799981688894314
End With
.DataBodyRange.FormatConditions(1).StopIfTrue = False
End With
Vielen dank an jeden der mir Antwort
Beste Grüße
Mathias

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

Betreff
Datum
Anwender
Anzeige
AW: Bedingtform. ListObjects -> Spaltenname
04.01.2023 09:41:01
ChrisL
Hi Mathias

With wsAktiv.ListObjects(1).ListColumns("Datum")
cu
Chris
AW: Bedingtform. ListObjects -> Spaltenname
04.01.2023 10:51:46
Mathias
Hallo Chris :)
Vielen Dank, das war jetzt super schnell und einfach. Hab den Code entsprechend geändert und es funktioniert. Wüsstest du ev. wie ich an den Buchstaben der jeweiligen Spalte komme. Um die Formel der bedingten Formatierung auch flexibel zu machen, wenn jemand die Spaltenreihenfolge ändert dann passt der Buchstabe in der Formel nicht mehr.
So schaut der Code jetzt aus:

    wsAktiv.Cells.FormatConditions.Delete
ez = wsAktiv.ListObjects(1).Range.Cells(1, 1).Row 'erste Zeile finden
On Error Resume Next
With wsAktiv.ListObjects(1) '.Select
.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=$O" & ez + 1 & "=""X"""
.DataBodyRange.FormatConditions(.DataBodyRange.FormatConditions.Count).SetFirstPriority
With .DataBodyRange.FormatConditions(1).Font
.Strikethrough = True
.Color = -16777024
End With
.DataBodyRange.FormatConditions(1).StopIfTrue = False
End With
With wsAktiv.ListObjects(1).ListColumns("Mitarbeiter")
.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ISTLEER($P" & ez + 1 & ")=FALSCH"
.DataBodyRange.FormatConditions(.DataBodyRange.FormatConditions.Count).SetFirstPriority
With .DataBodyRange.FormatConditions(1).Font
.Strikethrough = True
.Color = -11489280
End With
.DataBodyRange.FormatConditions(1).StopIfTrue = False
.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ISTLEER($A" & ez + 1 & ")=WAHR"
.DataBodyRange.FormatConditions(.DataBodyRange.FormatConditions.Count).SetFirstPriority
With .DataBodyRange.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13434879
.TintAndShade = 0
End With
.DataBodyRange.FormatConditions(1).StopIfTrue = False
End With
With wsAktiv.ListObjects(1).ListColumns("Datum")
.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ISTLEER($W" & ez + 1 & ")=WAHR"
.DataBodyRange.FormatConditions(.DataBodyRange.FormatConditions.Count).SetFirstPriority
With .DataBodyRange.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13434879
.TintAndShade = 0
End With
.DataBodyRange.FormatConditions(1).StopIfTrue = False
End With
With wsAktiv.ListObjects(1).ListColumns("Uhrzeit")
.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ISTLEER($S" & ez + 1 & ")=WAHR"
.DataBodyRange.FormatConditions(.DataBodyRange.FormatConditions.Count).SetFirstPriority
With .DataBodyRange.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13434879
.TintAndShade = 0
End With
.DataBodyRange.FormatConditions(1).StopIfTrue = False
End With
End Sub
Gibt es eigentlich eine einfachere Schreibweise im VBA für die bedingten Formatierungen oder geht es nur so.
Danke für die Hilfe
Grüße Mathias
Anzeige
AW: Bedingtform. ListObjects -> Spaltenname
04.01.2023 11:36:15
ChrisL
Hi

With wsAktiv.ListObjects(1).ListColumns("Mitarbeiter")
MsgBox "Spalte: " & Split(.Range.Address, "$")(1)
End With
cu
Chris
Geslöst-Bedingtform. ListObjects -> Spaltenname
04.01.2023 13:48:58
Mathias
Spitze :)))) es funktioniert.
@Chris vielen dank für deine Hilfe
Die Spalten können von ihrer Position in der Tabelle ohne Probleme innerhalb der Schnellformattabelle vertauscht werden. Es können auch beliebig viele Leerzeilen über der Tabelle eingesetzt werden.
Sollte jemand noch einen Verbesserungsvorschlag haben dann nur her damit aber im Prinzip funktioniert es. Nochmal Großes DANKE an Chris

Sub Bedingte_Formatierung_Flexibel()
Dim  ez As String
Dim wsAktiv As Worksheet
Set wsAktiv = ThisWorkbook.ActiveSheet
ez = wsAktiv.ListObjects(1).Range.Cells(1, 1).Row 'Nr der Überschrieftenzeile
On Error Resume Next
wsAktiv.Cells.FormatConditions.Delete
'Variante 1 Spalten Deklaration in Formel------------------------------------------------
With wsAktiv.ListObjects(1)
.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=$" & Split(wsAktiv.ListObjects(1).ListColumns("Gebucht").Range.Address, "$")(1) & ez + 1 & "=""X"""
.DataBodyRange.FormatConditions(.DataBodyRange.FormatConditions.Count).SetFirstPriority
With .DataBodyRange.FormatConditions(1).Font
.Strikethrough = True
.Color = -16777024
End With
.DataBodyRange.FormatConditions(1).StopIfTrue = False
End With
'Variante 2 Spalten Deklaration in With Anweisung---------------------------------------------------
With wsAktiv.ListObjects(1).ListColumns("Datum")
.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=ISTLEER($" & Split(.Range.Address, "$")(1) & ez + 1 & ")=WAHR"
.DataBodyRange.FormatConditions(.DataBodyRange.FormatConditions.Count).SetFirstPriority
With .DataBodyRange.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13434879
.TintAndShade = 0
End With
.DataBodyRange.FormatConditions(1).StopIfTrue = False
End With
End Sub
Beste Grüße
Mathias
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige