Deselect Chart in 2007

User avatar
ErikJan
BronzeLounger
Posts: 1255
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Deselect Chart in 2007

Post by ErikJan »

Need to deselect a chart on a chart-sheet in Excel 2007. Just clicking anywhere with the mouse does it. How do I do this in VBA?

If I record the 'click anywhere' action, I get nothing captured in VBA-code. Setting "Activewindow.Visible=False"is a known bug in 2007 and does not work. I tried ActiveChart.Deselect but that doesn't work too.

I can write code to prevent the chart from being selected, but I'd like my code to fix this anyway; maybe the user selected the chart manually, then I want to unselect.

Any ideas?

This works but I don't like it... SendKeys "{ESC}", TRUE

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Deselect Chart in 2007

Post by HansV »

Clicking "anywhere" with the mouse usually results in selecting something - if not a series or axis, then the chart's plot area or chart area. What exactly do you mean by deselect?
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1255
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Deselect Chart in 2007

Post by ErikJan »

When I click outside the chart-area all gets deselected.
Deselected.PNG
Selected.PNG
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Deselect Chart in 2007

Post by HansV »

Thanks. There doesn't seem to be any support for this, so the only thing I can come up with is the rather lame

Code: Select all

    ActiveChart.ChartArea.Select
    SendKeys "{ESC}"
Remark: this only works when run from Excel. If you run this from the Visual Basic Editor, the keystroke is sent to the VBE instead of to the chart.
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1255
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Deselect Chart in 2007

Post by ErikJan »

See my original post... it's there (I found that and had updated the post before the first answer already) :-)

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Deselect Chart in 2007

Post by HansV »

Sorry, I hadn't noticed the update. The screenshots in your reply had caused the original post to scroll off the screen.

I don't like SendKeys either, but I can't think of or find a better way.
Best wishes,
Hans

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Deselect Chart in 2007

Post by rory »

Does it work if you protect and then unprotect the chart?
Regards,
Rory

User avatar
ErikJan
BronzeLounger
Posts: 1255
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Deselect Chart in 2007

Post by ErikJan »

rory wrote:Does it work if you protect and then unprotect the chart?
Very interesting... that actually works! Thank you, I would have never figured that one out :clapping: