How to check if Chart Exist in Current Sheet

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, currently 'm using the code below, however i do not want to use error to check if chart exists in the sheet.
Please suggest better code. I just want to check if chart exist in active sheet irrespective of any name...

thanks in advance
Code:
[FONT=Courier New][FONT=Courier New]Option Explicit
Public chartexist As Boolean
[/FONT][/FONT]Sub CheckIfChartExist()
    On Error GoTo Err1
    ActiveSheet.ChartObjects(1).Activate
    chartexist = True
    Range("A1").Select
    Exit Sub
Err1:
    chartexist = False
    Err.Clear
End Sub
Sub TestFunction()
CheckIfChartExist
If chartexist = False Then
MsgBox "False"
Else
MsgBox "True"
End If
End Sub
 
Last edited:

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Case_Germany

Active Member
Joined
May 13, 2008
Messages
408
Hi, :)

maybe with this code:

Code:
Option Explicit
Sub Main()
    If fncChartExist Then
        MsgBox "Yes!"
    Else
        MsgBox "No!"
    End If
End Sub
Function fncChartExist() As Boolean
    Dim shpShape As Shape
    For Each shpShape In ActiveSheet.Shapes
        If shpShape.Type = msoChart Then fncChartExist = True: Exit Function
    Next shpShape
End Function
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,906
Code:
Function SomeChartExists(Optional onWorksheet as Worksheet) As boolean
    If onWorksheet Is Nothing then Set onWorksheet = ActiveSheet
    
    SomeChartExits = Not(onWorksheet.ChartObjects.Count = 0)
Exit Function
 

Watch MrExcel Video

Forum statistics

Threads
1,122,535
Messages
5,596,738
Members
414,094
Latest member
dingo_baby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top