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:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,207,089
Messages
6,076,518
Members
446,211
Latest member
b306750

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
Back
Top