Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1176to1180
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

Bedingte Formatierung per VBA

Bedingte Formatierung per VBA
Jörg
Guten Tag zusammen
Hoffe in diesem Forum Infos zu bekommen um folgendes Prob lösen zu können.
Es geht um das Erstellen von Bedingten Formaten per VBA.
Auf einem leeren Testblatt funtioniert das Modul einwandfrei. Sobald ich aber einen
benutzten Bereich festlege (z.B. A1:G88) und diesen mit Testdaten fülle schmiert
mir das Modul (Index ausserhalb des gültigen Bereichs) nach der 2. Formatierung ab.
Ändere ich den Anwendungsbereich aller 6 Formatierungen ab und beginne immer
in der Zelle A1 funktioniert das ganze wieder aber eben mit Bereichen die mir nichts nutzen.
Da ich unter Excel 2007 arbeite kann die max. 3 Bedingungen Regel nich das Hindernis sein.
Hat jemand von euch einen Hinweis oder Tipp.
Vielen Dank schonmal im vorraus für die Mühe
Gruß Jörg
Sub BedingteFormateErstellen()
Dim countFormat                           As Byte
Dim countColumn                          As Byte
Dim lastRow                                 As Byte
lastrow = Cells(65536, 1).End(xlup).Row
countColumn = ActiveSheet.Cells(1, 255).End(xlToLeft).Column
== Löschen vorhandenen Bedingten Formatierungen
If Cells.FormatConditions.Count >= 1 Then
Cells.FormatConditions.Delete
End If
== 1.Bedingung -
countFormat = countFormat + 1
Cells(1, 1).Activate
With Range(Cells(1, 1), Cells(lastRow, countColumn))
.FormatConditions.Add xlExpression, Formula1:="=$A1"""""
With .FormatConditions(countFormat)
.Priority = (countFormat)
.StopIfTrue = False
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThin
End With
End With
== 2.Bedingung -
countFormat = countFormat + 1
Cells(1, 1).Activate
With Range(Cells(1, 1), Cells(lastRow, 1))
.FormatConditions.Add xlExpression, Formula1:="=$A1"""""
With .FormatConditions(countFormat)
.Priority = (countFormat)
.StopIfTrue = False
.Interior.Pattern = xlPatternRectangularGradient
.Interior.Gradient.RectangleLeft = 0.5
.Interior.Gradient.RectangleRight = 0.5
.Interior.Gradient.RectangleTop = 0.5
.Interior.Gradient.RectangleBottom = 0.5
With .Interior.Gradient.ColorStops
.Clear
With .Add(0)
.ThemeColor = xlThemeColorDark1
End With
With .Add(1)
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599993896298105
End With
End With
End With
End With
== 3.Bedingung -
countFormat = countFormat + 1
Cells(1, 2).Activate
With Range(Cells(1, 2), Cells(lastRow, countColumn))
.FormatConditions.Add xlExpression, Formula1:="=BlaBlaBla"
With .FormatConditions(countFormat)
.Priority = (countFormat)
.StopIfTrue = False
.Interior.Pattern = xlPatternRectangularGradient
.Interior.Gradient.RectangleLeft = 0.5
.Interior.Gradient.RectangleRight = 0.5
.Interior.Gradient.RectangleTop = 0.5
.Interior.Gradient.RectangleBottom = 0.5
With .Interior.Gradient.ColorStops
.Clear
With .Add(0)
.Color = 13434879
End With
With .Add(1)
.Color = 13311
End With
End With
End With
End With
== 4.Bedingung -
countFormat = countFormat + 1
Cells(1, 2).Activate
With Range(Cells(1, 2), Cells(lastRow, countColumn))
.FormatConditions.Add xlExpression, Formula1:="=BlaBlaBla"
With .FormatConditions(countFormat)
.Priority = (countFormat)
.StopIfTrue = False
.Interior.Pattern = xlPatternRectangularGradient
.Interior.Gradient.RectangleLeft = 0.5
.Interior.Gradient.RectangleRight = 0.5
.Interior.Gradient.RectangleTop = 0.5
.Interior.Gradient.RectangleBottom = 0.5
With .Interior.Gradient.ColorStops
.Clear
With .Add(0)
.Color = 13434879
End With
With .Add(1)
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0.400006103701895
End With
End With
End With
End With
== Festlegen der 5.Bedingung -
countFormat = countFormat + 1
Cells(4, 2).Activate
With Range(Cells(4, 2), Cells(lastRow, countColumn))
.FormatConditions.Add xlExpression, Formula1:="=BlaBlaBla"
With .FormatConditions(countFormat)
.Priority = (countFormat)
.StopIfTrue = False
.Interior.Pattern = xlPatternRectangularGradient
.Interior.Gradient.RectangleLeft = 0.5
.Interior.Gradient.RectangleRight = 0.5
.Interior.Gradient.RectangleTop = 0.5
.Interior.Gradient.RectangleBottom = 0.5
With .Interior.Gradient.ColorStops
.Clear
With .Add(0)
.Color = 13434879
End With
With .Add(1)
.Color = 49407
End With
End With
End With
End With
== Festlegen der 6.Bedingung -
countFormat = countFormat + 1
Cells(4, 2).Activate
With Range(Cells(4, 2), Cells(lastRow, countColumn))
.FormatConditions.Add xlExpression, Formula1:="=BlaBlaBla"
With .FormatConditions(countFormat)
.Priority = (countFormat)
.StopIfTrue = False
.Interior.Pattern = xlPatternRectangularGradient
.Interior.Gradient.RectangleLeft = 0.5
.Interior.Gradient.RectangleRight = 0.5
.Interior.Gradient.RectangleTop = 0.5
.Interior.Gradient.RectangleBottom = 0.5
With .Interior.Gradient.ColorStops
.Clear
With .Add(0)
.Color = 13434879
End With
With .Add(1)
.Color = 49407
End With
End With
End With
End With
End Sub

AW: Bedingte Formatierung per VBA
21.09.2010 07:40:23
Heiti
Womit rufst du diese Funktion auf, wenn ich fragen darf?
AW: Bedingte Formatierung per VBA
21.09.2010 16:11:34
Jörg
Hallo Heiti
Diesen Code habe ich nur zu Testzwecken als eigenständiges Modul angelegt.
Der Code ist Teil eines anderen Moduls.
Gruß Jörg
Welchen Wert hat der NAME BlaBlaBla? orT
21.09.2010 10:11:42
Luc:-?
Gruß Luc :-?
AW: Welchen Wert hat der NAME BlaBlaBla? orT
21.09.2010 16:16:40
Jörg
Hallo Luc
Habe die eigentlichen Formeln wegen der Übersichtlichkeit weggelassen.
Es handelt sich um Informations-Funktionen wie Z.B. IstLeer.
Aber wie im Thread erwähnt wenn alle "Angewendet auf"-Bereiche in A1 beginnen funktioniert der Code fehlerfrei. Nur die Bereich nutzen mir nichts.
Gruß Jörg
Welche Zelle ist denn selektiert, wenn...
21.09.2010 20:12:44
Luc:-?
…der Code läuft, Jörg?
XL hat da nämlich 'ne gemeine Falle — Autoanpassung an die selektierte Zelle…
Gruß Luc :-?
Anzeige
AW: Welche Zelle ist denn selektiert, wenn...
21.09.2010 21:01:02
Jörg
Hallo Luc
Ich hab folgendes probiert allerdings ohne Erfolg.
Egal welche Zelle ich vor dem ersten With-Block der den Range festlegt aktiviere oder selektiere
der Code läuft nur fehlerfrei durch wenn der Range in der Zelle A1 beginnt.
AW: Bedingte Formatierung per VBA
23.09.2010 14:46:42
Jörg
Hallo zusammen
Ich will noch nicht aufgeben. Habe weiter getestet und folgende Entdeckung gemacht,
die ich mir von der Logik her nicht erklären kann, die aber das Problem
"Index außerhalb des gültigen Bereichs" auslöst.
Das Modul schmiert nachdem die 3. Bedingung angelegt wurde an der Stelle
"With .FormatConditions(countFormat)" ab.
Nun hab ich mal ein Debug.Print ...Count dazwischengesetzt und festgestellt,
das zu diesem Zeitpunkt 3 Bedingungen erstellt sind.
Die 3. Bedingung ist aber genau wie die zweite mit dem Wert "2" indiziert.
Da die Variable CountFormat aber richtigerweise den Wert "3" enthält gibt
es die Index-Fehlermeldung.
Warum indiziert VBA hier nicht korrekt?
Danke nochmal im vorraus für eure Mühe und Tipps.
Gruß Jörg
Anzeige
AW: Bedingte Formatierung per VBA
25.09.2010 18:05:59
Tino
Hallo,
habe Deinen Code nur mal so überflogen, kann sein das da noch mehr nicht passt.
Versuch mal und ändere die Code Zeile
countFormat = countFormat + 1

durch diese ab.
countFormat = .FormatConditions.Count

diese setzt Du nach
.FormatConditions.Add …

ein.
Gruß Tino
AW: Bedingte Formatierung per VBA
25.09.2010 21:52:46
Jörg
Hallo Tino
Vielen Dank für den Tipp. Das passt.
Der Code läuft nun fehlerfrei durch.
"countFormat" zeigt sowohl bei der 2. und der 3. Formatierung den Wert 2 an und positioniert
das 2. Format an das Ende der Liste.
Irgendwie unlogisch aber unproblematisch.
Also nochmal vielen Dank und schönen Wochenende.
Gruß Jörg
Anzeige
AW: Bedingte Formatierung per VBA
26.09.2010 11:39:24
Tino
Hallo,
weil Dein Zähler nicht mit der Anzahl Bedinger Formatierungen in dem Bereich übereinstimmt.
Zähler hat z. Bsp. den Wert 3 aber der Bereich hat erst 2 Bedingungen.
Gruß Tino
AW: Bedingte Formatierung per VBA
26.09.2010 20:33:22
Jörg
Hallo Tino
Danke für die Antwort.
Welche Möglichkeit (Count) gibt es denn alle Bed. Formate eines Sheets zu zählen egal in welchem Bereich sie gültig sind?
Gruß Jörg
AW: Bedingte Formatierung per VBA
26.09.2010 20:39:03
Hajo_Zi
Hallo Jörg,
A = Cells.SpecialCells(xlCellTypeAllFormatConditions).Count ' bedingte Formate

AW: Bedingte Formatierung per VBA
26.09.2010 20:55:17
Jörg
Hallo Hajo
Ich würde mal sagen. Das war es dann. Damit ist mein Prob zu 100% gelöst.
Vielen Dank an alle die mir geholfen haben.
Schönen Abend noch
Gruß Jörg
Anzeige

305 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige