Seaching for Charts Using the Chart Title : Chart Title « Excel « VBA / Excel / Access / Word






Seaching for Charts Using the Chart Title

 
Function GetChartByCaption(ws As Worksheet, sCaption As String) As Chart
    Dim myChart As ChartObject
    Dim myChart As Chart
    Dim sTitle As String

    Set myChart = Nothing
    For Each myChart In ws.ChartObjects
        If myChart.Chart.HasTitle Then
            sTitle = myChart.Chart.ChartTitle.Caption
            If StrComp(sTitle, sCaption, vbTextCompare) = 0 Then
                Set myChart = myChart.Chart
                Exit For
            End If
        End If
    Next
    Set GetChartByCaption = myChart
    Set myChart = Nothing
    Set myChart = Nothing
End Function
Sub TestGetChartByCaption()
    Dim myChart As Chart
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set myChart = GetChartByCaption(ws, "I am the Chart Title")

    If Not myChart Is Nothing Then
        Debug.Print "Found chart"
    Else
        Debug.Print "Sorry - chart not found"
    End If

    Set ws = Nothing
    Set myChart = Nothing
End Sub

 








Related examples in the same category

1.Adding a Chart Title
2.To change chart default title, set the Text property of the ChartTitle object, which represents the chart title
3.To position the title, set its Top property (specifying the number of points from the top edge of the worksheet) and its Left property (specifying the number of points from the left edge of the worksheet)
4.To format the text of the title, work with its Font object