Solved

Excel Macro runs and selects charts after ending

Posted on 2011-02-17
10
546 Views
Last Modified: 2012-05-11
When I run a macro to populate charts in Excel 2007 the charts revert to "selected" and I have to go in and unselect them using esc.

Is there a function or something I can call to have it not exhibit this behavior?

This issue does not happen in excel 2003.

See attached image for what I mean.

Chart selected:

   Selected
Chart unselected:

 Unselected
0
Comment
Question by:jason987
  • 4
  • 3
  • 3
10 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 250 total points
ID: 34921486
Yes. Take out the Select line in the macro.
Instead of
Chart1.Select
Selection.DoSomething
Just use
Chart1.DoSomething
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 34921506
Or, at the end of the macro do a

Range("A1").Select

cheers, teylyn
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34921510
Or the same thing with Chart1.Activate and ActiveChart
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 5

Author Comment

by:jason987
ID: 34921818
Is there a reason "ActiveChart.Deselect"  doesn't work?
0
 
LVL 50

Assisted Solution

by:Ingeborg Hawighorst
Ingeborg Hawighorst earned 250 total points
ID: 34921927
That seems to be something that's not working correctly. Even MVP Jon Peltier couldn't get it to work and uses this workaround:

I've noticed this. I think I got around it by using

Worksheets("Sheet1").Activate
Worksheets("Sheet1").Range("A1").Select

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com

source: http://www.officekb.com/Uwe/Forum.aspx/excel-prog/113539/xl2007-problem-with-Activechart-Deselect

So, like I suggested above. Select a cell when the macro is finished.

cheers, teylyn
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34922047
The simple solution is to stop activating charts. There are some things that are easier to do with ActiveChart then going directly from the chart object, but you can do it. If you need help finding all the right commands, post your code and we'll help you out.
0
 
LVL 5

Author Comment

by:jason987
ID: 34922081
Sorry I should have posted code in the first place:

    Sheets("Data Chart").Select
    ActiveChart.ChartArea.Select

    ActiveChart.SeriesCollection(1).XValues = Sheets("Data").Range("A3:A" & SomeVar)
    ActiveChart.SeriesCollection(1).Values = Sheets("Data").Range("E3:E" & SomeVar)
    ActiveChart.SeriesCollection(1).Name = Sheets("Data").Range("E2")

    ActiveChart.Deselect    'deselect chart

Open in new window


I tried

    Sheets("Data Chart").Select
    ActiveChart.ChartArea.Select

    ActiveChart.SeriesCollection(1).XValues = Sheets("Data").Range("A3:A" & SomeVar)
    ActiveChart.SeriesCollection(1).Values = Sheets("Data").Range("E3:E" & SomeVar)
    ActiveChart.SeriesCollection(1).Name = Sheets("Data").Range("E2")

    Range("A1").Select
    ActiveChart.Deselect    'deselect chart

Open in new window


And it errored on the range line

I also tried taking out the selection and working with the chart by name and it had no affect.

    Sheets("Data Chart").SeriesCollection(1).XValues = Sheets("Data").Range("A3:A" & SomeVar)
    Sheets("Data Chart").SeriesCollection(1).Values = Sheets("Data").Range("E3:E" & SomeVar)
    Sheets("Data Chart").SeriesCollection(1).Name = Sheets("Data").Range("E2")

Open in new window

0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 34922123
Hello,

there are some problems in the way you reference the chart objects. See this link http://peltiertech.com/Excel/ChartsHowTo/QuickChartVBA.html for some best practice on how to create and manipulate charts. It's standard code and once you understand how it all works together, you'll be much better at coding chart VBA.

cheers, teylyn
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34922158
Mine works fine like this
    Charts("Data Chart").SeriesCollection(1).XValues = Sheets("Data").Range("A3:A" & SomeVar)
    Charts("Data Chart").SeriesCollection(1).Values = Sheets("Data").Range("E3:E" & SomeVar)
    Charts("Data Chart").SeriesCollection(1).Name = Sheets("Data").Range("E2")

Open in new window

0
 
LVL 5

Author Comment

by:jason987
ID: 34922234
Uggh, it was saved selected so my code changes weren't affecting it.

Thanks tommy and teylyn.  I'll split since they are both valid approaches and deselect is definitely broken.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

809 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