Check whether a named Chart 'Chart_01' is currently selected on the active worksheet

Posted on 2011-05-11
Last Modified: 2012-06-21
Dear Experts:

I got lots of column charts on the active worksheet.

The Charts are named 'Chart_01', 'Chart_02', 'Chart_03' and so forth.

I would like to have a code snippet with the following requirements:

The macro is to check whether the first chart, i.e. 'Chart_01' is selected. If yes or no, either way, a msgbox has to say so, i.e. either 'Chart_01' is selected or 'Chart_01' is not selected.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Question by:AndreasHermle
    LVL 18

    Assisted Solution


    Something this would help you

    Dim chtChart  As Chart
        On Error Resume Next
        Set chtChart = ActiveChart
        On Error GoTo 0
        If Not chtChart Is Nothing Then
            MsgBox "Activechart's Name is " & Trim$(Replace(chtChart.Name, ActiveSheet.Name, ""))
            MsgBox "No chart selected ", vbInformation
        End If

    LVL 7

    Accepted Solution

    Sub isChart1Selected()
        If Selection.Parent.Name = ActiveSheet.Name & " Chart_01" Then
            MsgBox "Chart_01 is selected"
            MsgBox "Chart_01 is NOT selected"
        End If
    End Sub

    Works in Excel 2010

    Author Comment

    Dear both,

    thank you very much for your swift response. I will check you codes tomorrow and then get back to you with a feedback.

    Regards, Andreas

    Author Comment

    Hi m4trix,

    yours is working just fine. Thank you very much.

    Hi Kris, I am afraid to tell you that yours is somehow not working. Any idea why.

    Regards, Andreas
    LVL 43

    Expert Comment

    by:Saqib Husain, Syed
    Hi Andreas, although I am not working on this one, a piece of advice is that "not working" does not help as far as troubleshooting is concerned. You could help Kris by specifiying where and how it does not work. Does not work could mean any or more of

    - Runs but gives no results
    - Runs and gives results but the results are wrong
    - There are errors before running the code
    - Runs but breaks (in which case you should specify the line number where it breaks)

    Author Comment

    Dear ssagibh:

    you are absolutely right. I was in a hurry and did not specify where and how it does not work. Sorry about that!

    And moreover, checking on Kris' code again, I noticed that his code works as well. I inadvertently ran another code instead of Kris' macro.

    I really appreciate the professionalism of all  of you.

    Regards, Andreas

    Author Closing Comment

    Hi Kris and m4trix:

    both code work just fine and use different approaches. This is a good way to learn from you.

    Thank you very much for your professional support.

    Regards, Andreas

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
    This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
    Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
    This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now