Saturday, November 28, 2015

Excel VBA code to determine if chart is exists

Many a times it is very difficult to determine if chart is exists. Also it is very time consuming to check all sheets in workbook to check for a specific chart. So, checking charts in project will be a one of the data validation and performing this activity is manually is a quire tedious job. But, we can make this task very simple by using a short code which will help us to check if chart is exists.

By using VBA code in project to validate charts existence is very powerful and accurate method for graphical data representation. Which certainly saves a huge amount of time while data validation.


Here is the code which will easily validate if chart/graph is exists in Workbook:

Sub Chart_Is_Exists()

' Author: Dreams24
' Written for VBA Tricks and tips blog
https://vbatricksntips.com

Dim i As Integer
Dim nChart As String

' Name of chart to check

nChart = "Test_Chart"

'for loop to check chart existence in each sheet


For i = 1 To ThisWorkbook.Sheets.Count    

      On Error Resume Next        
      Sheets(i).Activate    Sheets(i).ChartObjects(nChart).Activate        
        If Err.Number <> 0 Then        
             MsgBox "No Chart Exists In " & ActiveSheet.Name, vbInformation       
             Err.Clear    
        Else        
             MsgBox "A Chart Does Exist In " & ActiveSheet.Name, vbInformation    
        End If
Next i

End Sub

No comments: