Sub ChangeDataLabelFontSize()
'Will change the font size for all data labels on the active chart
Dim x As Integer
Dim SizeNew As Variant
Dim myChart As Chart
Dim Name As String
Dim SizeOrig As Integer
Dim Response As Variant
Dim ser As Series
'define chart
Set myChart = ActiveChart
'confirm that a chart has been selected
If myChart Is Nothing Then
Response = MsgBox("You must select a chart before using this macro.", vbOKOnly, "No Chart Selected")
Exit Sub
End If
'define chart name
Name = myChart.Name
SizeOrig = myChart.SeriesCollection(1).DataLabels.Font.Size
'request font size
SizeNew = InputBox("This macro will change the font size for all " _
& "data labels on the active chart. The current chart is:" _
& vbNewLine & vbNewLine & Name & vbNewLine & vbNewLine _
& "To continue, enter the font size below. " _
& "To leave " & Name & " unchanged, click Cancel.", "Enter Font Size")
'confirm if user clicked cancel or didn't enter anything and clicked ok
If SizeNew = "" Then
MsgBox ("Exiting macro, no changes made.")
Exit Sub
End If
'confirm that user entered a number
If Not IsNumeric(SizeNew) Then
MsgBox ("Please enter only numeric values. No changes made.")
Exit Sub
End If
'confirm that font size is in range
If SizeNew < 6 Or SizeNew > 18 Then
Response = MsgBox("You have entered a font size of " & SizeNew _
& ". Are you sure? Click Ok to continue, or Cancel to leave " _
& "your chart unchanged.", vbOKCancel, "Font Size Looks Odd")
If Response = vbCancel Then
MsgBox ("Exiting macro, no changes made.")
Exit Sub
End If
End If
'change data label size for each series that has data labels
For x = 1 To myChart.SeriesCollection.Count
If myChart.SeriesCollection(x).HasDataLabels Then
myChart.SeriesCollection(x).DataLabels.Font.Size = SizeNew
End If
Next x
'redraw chart on screen (not sure why this isn't automatic, but it isn't)
ActiveSheet.Calculate
ActiveSheet.Range("CA1").ClearContents 'no idea why this works, but it does
myChart.Refresh
'give option to revert to original size if one exists
If Not IsNumeric(SizeOrig) Then Exit Sub 'confirm that we stored a starting font size
Response = MsgBox("I have changed the data label font size to " & _
SizeNew & ". To accept this change, click OK. " & _
"To revert to your original size of " & SizeOrig & _
", click Cancel.", vbOKCancel, "Confirm Change")
If Response = vbCancel Then
For x = 1 To myChart.SeriesCollection.Count
If myChart.SeriesCollection(x).HasDataLabels Then
myChart.SeriesCollection(x).DataLabels.Font.Size = SizeOrig
End If
Next x
ActiveSheet.Calculate
ActiveSheet.Range("CA1").ClearContents 'no idea why this works, but it does
myChart.Refresh
MsgBox ("I have changed the font size back to " & _
SizeOrig & ". Exiting macro.")
End If
End Sub