Solved

Excel Macro runs and selects charts after ending

Posted on 2011-02-17
10
543 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:teylyn
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
 
LVL 5

Author Comment

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

Assisted Solution

by:teylyn
teylyn 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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:teylyn
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

11 Experts available now in Live!

Get 1:1 Help Now