Solved

Excel Macro runs and selects charts after ending

Posted on 2011-02-17
10
547 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

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

735 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