Solved

Excel Macro runs and selects charts after ending

Posted on 2011-02-17
10
549 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 (Microsoft MVP / EE MVE)
Ingeborg Hawighorst (Microsoft MVP / EE MVE) 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

623 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