wolfsjux
Goto Top

Bedingte Formatierung mit VBA (Excel 5) als Variable

Hallo Experten,

habe folgende, funktionierende Befehlsfolge in VBA (Excel 5), die möchte ich aber gerne variabel gestalten, um sie in vielen Sub's nutzen zu können.

'Beispiel jeweilige, Vorbereitung individuell
'BegRow = 3 'ohne Überschrift
'EndRow = iMaxR 'Ist 55, in laufender Sub ermittelt
'BegCell = "$c" & sDZ & StartRow
'EndCell = "$c" & sDZ & EndRow
'sv2 = StartCell & ":" & EndCell 'Ergebnis muss endgültig so aussehen =MAX("$c$3:$c$55")
'Call Unter_Sub(sv2)

Sub Unter_Sub(sv2)
Range(sv2).Select 'Der Befehl funktioniert so und positioniert richtig, obwohl die "$" dort unnötig sind
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=MAX($C$3:$C$55)" 'habe in Variabler Namens sv2 folgenden Wert stehen: "$c$3:$c$8"; wie kann ich die einsetzen
With Selection.FormatConditions(1).Font 'einfach zwischen die Klammern (sv2) geht nicht, auch so nicht (& sv2) oder so sv2
.Bold = True 'auch mit der Anweisung ab "=MAX(... habe ich es schon versucht!
.Italic = False
.ColorIndex = 3 'fettes rot
End With
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=MIN($C$3:$C$55)"
With Selection.FormatConditions(2).Font
.Bold = True
.Italic = False
.ColorIndex = 10 'fettes grün
End With

Kann jemand weiterhelfen? Aber bitte keine Vermutungen oder Hinweise auf alten Excel. Ich bin älter als Excel.

Danke im Voraus
Wolf
Kommentar vom Moderator tomolpi am 10.02.2021 um 19:01:07 Uhr
Titel korrigiert

Content-Key: 650203

Url: https://administrator.de/contentid/650203

Ausgedruckt am: 29.03.2024 um 06:03 Uhr

Mitglied: Xolger
Lösung Xolger 10.02.2021 um 20:44:53 Uhr
Goto Top
Hallo Wolfsjux,

Sub Test()

Dim myString As String
Const sv2 = "$c$3:$c$55"  

myString = "=MAX(" & sv2 & ")"  


Range("A3:E55").Select  
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=myString
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.ColorIndex = 3
End With
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=MIN($C$3:$C$55)"  
With Selection.FormatConditions(2).Font
.Bold = True
.Italic = False
.ColorIndex = 10
End With
End Sub

Mein Vorschlag.


Gruß
Xolger
Mitglied: Wolfsjux
Wolfsjux 10.02.2021 um 21:35:27 Uhr
Goto Top
Danke Xolger,

das lief schon mal ohne Fehlermeldung durch, allerdings ist das erhoffte Ergebnis ausgeblieben. In beiden Feldern, wo sonst "=Max(... und "=MIN(... stand, steht jetzt nur noch "WAHR". Abgesehen davon, dass ich zwei verschiedene Strings für MIN und MAX brauchte, werde ich noch etwas experimentieren.

Aber ein Anfang ist gemacht. Vielleicht fällt dir ja noch was ein.

Liebe Grüße
Wolf
Mitglied: Wolfsjux
Wolfsjux 10.02.2021 um 22:30:52 Uhr
Goto Top
Nochmals Dank an Xolger,

jetzt klappt alles bestens. Ich schließe dich ins Nachtgebet ein.

Nochmals beste Grüße
Wolf