AddLabel
Folgendes VBA-Unterprogramm SetDateInDiagram() fügt in alle Charts einer Excel-Datei ein Textfeld / Label rechts oben mit dem aktuellen Monat (Januar, Februar ...) und Jahr (vierstellig) ein. Mit der Funktion CheckLabelExists(...) wird geprüft, ob ein Label mit dem Namen "Stand" bereits existiert.
Private Function CheckLabelExists(argChart As Object, argName As String) As Boolean
Dim obj As Object
CheckLabelExists = False
For Each obj In argChart.Shapes
If UCase(obj.Name) = UCase(argName) Then CheckLabelExists = True: Exit Function
Next obj
End Function
Sub SetDateInDiagram()
'
' Stand Makro
' Einfügen des Standes des Diagrames
'
Dim mySheet As Worksheet
Dim myChart As Chart
For Each mySheet In Worksheets
If mySheet.ChartObjects.Count > 0 Then
Set myChart = mySheet.ChartObjects(1).Chart
If Not CheckLabelExists(myChart, "Stand") Then
With myChart.Shapes.AddLabel(msoTextOrientationHorizontal, 380, 4, 144, 20)
.Name = "Stand"
.TextFrame.Characters.Text = Format(Date, "mmmm yyyy")
.TextFrame.Characters.Font.Size = 12
.TextFrame.Characters.Font.Bold = True
.TextFrame.HorizontalAlignment = xlHAlignRight
End With
Else
myChart.Shapes("Stand").TextFrame.Characters.Text = Format(Date, "mmmm yyyy")
myChart.Shapes("Stand").TextFrame.HorizontalAlignment = xlHAlignRight
End If
End If
Next
End Sub