Solved

Excel Macro runs and selects charts after ending

Posted on 2011-02-17
10
545 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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