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

Forumthread: VBA-> Datumsabhänige Formatierung

VBA-> Datumsabhänige Formatierung
16.02.2022 09:21:49
Juli
Hallo!
Folgendes "Problem" liegt vor:
(Beispieltabelle liegt hier: https://www.herber.de/bbs/user/151158.xlsm)
Ich habe eine Tabelle in der alle Zeilen mit einem Datum (Spalte B) versehen sind, hier ist eine bedingte Formatierung nötig, die mir ausgiebt ob das Datum schon in der Vergangenheit, oder innerhalb der nächsten 10 oder 14 Tage liegt.
(Die klassische bedingte Formatierung hat sich leider nicht als so ganz praktisch auf Dauer erwiesen, da die Liste nicht gerade statisch ist)
Ich bekomme es hin, den Wert anhant einer zweiten Spalte D einzufärben, wo die Differenz in Tagen berechnet wird, aber nicht in der Datumsspalte selber.
Würde mir den Umweg über Spalte D gene sparen.
Kann ich also meine Formula1:="14" durch etwas wie Formula1:="DATUMSDIFFERENZ IN TAGEN = 14" ersetzen?
Vielen Dank schon mal?

Sub WerteEinfärben()
Dim rngBereich As Range
Dim rngBereich2 As Range
With Tabelle1
Set rngBereich = .Range("D:D") 'definiert Bereich'
rngBereich.FormatConditions.Delete 'löscht vorherige Formatierung'
rngBereich.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="14" 'Setzt Bedingung für neue Formatierung'
rngBereich.FormatConditions(rngBereich.FormatConditions.Count).SetFirstPriority
With rngBereich.FormatConditions(1)
.Font.ColorIndex = 10 'Schriftfarbe'
.Interior.ColorIndex = 35 'Hintergrundfarbe'
End With
Set rngBereich = .Range("D:D")
rngBereich.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, Formula1:="14"
rngBereich.FormatConditions(rngBereich.FormatConditions.Count).SetFirstPriority
With rngBereich.FormatConditions(1)
.Font.ColorIndex = 29
.Interior.ColorIndex = 24
End With
Set rngBereich = .Range("D:D")
rngBereich.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, Formula1:="10"
rngBereich.FormatConditions(rngBereich.FormatConditions.Count).SetFirstPriority
With rngBereich.FormatConditions(1)
.Font.ColorIndex = 49
.Interior.ColorIndex = 34
End With
Set rngBereich = .Range("D:D")
rngBereich.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, Formula1:="0"
rngBereich.FormatConditions(rngBereich.FormatConditions.Count).SetFirstPriority
With rngBereich.FormatConditions(1)
.Font.ColorIndex = 3
.Interior.ColorIndex = 38
End With
End With
End Sub
Anzeige

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

Betreff
Datum
Anwender
Anzeige
AW: VBA-> Datumsabhänige Formatierung
16.02.2022 09:48:12
UweD
Hallo
versuch es mal so

Sub WerteEinfärben()
Dim rngBereich As Range
Dim rngBereich2 As Range
With Tabelle1
Set rngBereich = .Range("B:B") 'definiert Bereich'
rngBereich.FormatConditions.Delete 'löscht vorherige Formatierung'
rngBereich.FormatConditions.Add Type:=xlExpression, Formula1:="=(B1"""")*(HEUTE()-B1>14)" 'Setzt Bedingung für neue Formatierung'
rngBereich.FormatConditions(rngBereich.FormatConditions.Count).SetFirstPriority
With rngBereich.FormatConditions(1)
.Font.ColorIndex = 10 'Schriftfarbe'
.Interior.ColorIndex = 35 'Hintergrundfarbe'
End With
rngBereich.FormatConditions.Add Type:=xlExpression, Formula1:="=(B1"""")*(HEUTE()-B1"""")*(HEUTE()-B1"""")*(HEUTE()-B1
LG UweD
Anzeige
AW: VBA-> Datumsabhänige Formatierung
16.02.2022 10:36:29
Juli
Das funktioniert zumindest vom Grundprinzip, aber die Ergebnisse der Berechnung der Tagesdifferenz sind irgendwie falsch.
Zumindest wendet er die Farben auf die falschen felder an.

=(B1"""")*(HEUTE()-B1>14)
Wofür ist der erste Teil vor dem * genau gedacht ?
Anzeige
AW: VBA-> Datumsabhänige Formatierung
16.02.2022 10:46:07
Juli
Habs jetzt über

=((B1-HEUTE())>14)
gelöst bekommen,
DANKE!
AW: VBA-> Datumsabhänige Formatierung
16.02.2022 11:40:45
UweD
Hallo nochmal
Der erste Teil der Formel war dafür gedacht, wenn kein Datum in Spalte B steht ( in deinem Bespiel ab Zeile12 ) dass dann keine Farbe angezeigt wird.
So müsste das nun laufen ( hatte Klammern falsch gesetzt )

Sub WerteEinfärben()
Dim rngBereich As Range
Dim rngBereich2 As Range
With Tabelle1
Set rngBereich = .Range("B:B") 'definiert Bereich'
rngBereich.FormatConditions.Delete 'löscht vorherige Formatierung'
rngBereich.FormatConditions.Add Type:=xlExpression, Formula1:="=(B1"""")*((HEUTE()-B1)>14)" 'Setzt Bedingung für neue Formatierung'
rngBereich.FormatConditions(rngBereich.FormatConditions.Count).SetFirstPriority
With rngBereich.FormatConditions(1)
.Font.ColorIndex = 10 'Schriftfarbe'
.Interior.ColorIndex = 35 'Hintergrundfarbe'
End With
rngBereich.FormatConditions.Add Type:=xlExpression, Formula1:="=(B1"""")*((HEUTE()-B1)"""")*((HEUTE()-B1)"""")*((HEUTE()-B1)
LG UweD
Anzeige
;

Forumthreads zu verwandten Themen

Anzeige
Anzeige
Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige