Zellformat mit VBA inkl. Datenbalken
04.10.2017 19:25:56
Christian
ich bin nach langer Abstinenz mal wieder mit VBA beschäftigt. Eins noch vorab, meine VBA Kenntnisse sind bescheiden :)
Zu meinem Problem:
Ich habe eine Zelle A10, in der insgesamt 3 Werte stehen sollen. Wert1 ist der aktuelle Ist-Wert, Wert2 ist der Maximale Wert und Wert3 ist das Verhältnis in %. Ist und Max wert sind mit "/" getrennt und der %-Wert steht in Klammern. Das ganze sieht dann ungefähr so aus:
321.264 / 321.264 (100%)
Alle Werte sind Variablen. Der Ist-Wert ändert sich ständig, der Max-Wert ändert sich nur bei einem neuen Szenario (Also auch eine Variable die für den aktuellen Fall aber konstant ist) und Wert3 ändert sich logischerweise mit dem Ist-Wert.
Die Zelle soll zudem das Verhältnis mit einem Datenbalken grafisch darstellen.
Hier ist mein Code für das Zellformat:
Wert3 = Round(Wert1 / Wert2 * 100, 0) 'Verhältnis
numFormat = "#,##"" / """ & Wert2 & " (" & Wert3 & "%)"
Worksheets(1).Cells(10, 1).NumberFormat = numFormat
Und hier der Datenbalken:
Worksheets(1).Cells(10, 1).FormatConditions.AddDatabar
Worksheets(1).Cells(10, 1).FormatConditions(Worksheets(1).Cells(10, 1).FormatConditions.Count).ShowValue = True
With Worksheets(1).Cells(10, 1).FormatConditions(1)
.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=0
.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=Wert2
End With
With Worksheets(1).Cells(10, 1).FormatConditions(1).BarColor
.Color = 255
.TintAndShade = 0
End With
Worksheets(1).Cells(10, 1).FormatConditions(1).BarFillType = xlDataBarFillSolid
Worksheets(1).Cells(10, 1).FormatConditions(1).Direction = xlContext
Worksheets(1).Cells(10, 1).FormatConditions(1).NegativeBarFormat.ColorType = xlDataBarColor
Worksheets(1).Cells(10, 1).FormatConditions(1).BarBorder.Type = xlDataBarBorderNone
Worksheets(1).Cells(10, 1).FormatConditions(1).AxisPosition = xlDataBarAxisAutomatic
With Worksheets(1).Cells(10, 1).FormatConditions(1).AxisColor
.Color = 0
.TintAndShade = 0
End With
With Worksheets(1).Cells(10, 1).FormatConditions(1).NegativeBarFormat.Color
.Color = 255
.TintAndShade = 0
End With
Der Datenbalken an sich funktioniert, er stellt das Verhältnis je nach eingetragenem Wert1 auch richtig dar. Das Problem ist die Darstellung der Zahlen in der Zelle.
Bsp.: So soll es aussehen:
234.612 / 234.612 (100%)
So sieht es aus:
23.461.2 / 234612 (100%)
Enthält die erste Zahl hinten eine oder mehrere Nullen werden diese einfach weggelassen.
Bsp.: So soll es aussehen:
320.000 / 320.000 (100%)
So sieht es aus:
32. / 32000.0 (100%)
Ich komme da mit meinem Know-How einfach nicht weiter. Ich hoffe mein anliegen ist verständlich formuliert. Danke für die Aufmerksamkeit und eventuelle Lösungsvorschläge.
Gruß Christian