dwirony
dwirony

Reputation: 5450

Deselecting an active chart object in Excel without using SendKeys

Running into a small issue that I can't figure out. I have a small ComboBox1_Change event where I loop through all the ActiveSheet.ChartObjects and make some changes based on a ComboBox1.Value.

However, after the code finishes running, the selection is still focused on the last chart object on my sheet (see the 4 small circles on the border of the chart):

img1

I can't figure out how to lose this selection. I've tried several things (Range("A1").Select, Range("A1").Activate, ActiveChart.Deselect, ActiveChart.ChartObjects.Deselect) but none of these work within the subroutine (If I run Range("A1").Select in another sub after this one, then I successfully deselect the chart object.)

I can deselect the chart object by using SendKeys, but I'd really like to avoid that. There must be another way to make this work - Any suggestions?

Code:

Private Sub ComboBox1_Change()

Application.ScreenUpdating = False

For Each cht In ActiveSheet.ChartObjects
    cht.Activate
    ActiveChart.ChartTitle.Text = ComboBox1.Value
    'Some other stuff, excluded.
Next cht

'Range("A1").Activate  -  doesn't work

'Range("A1").Select  -  doesn't work

'ActiveChart.Deselect  -  doesn't work

'ActiveChart.ChartObjects.Deselect  -  doesn't work, throws error

'SendKeys "{ESC}"  - works, but I'd rather not.

Application.ScreenUpdating = True

End Sub

Upvotes: 0

Views: 1425

Answers (1)

jblood94
jblood94

Reputation: 17011

I can't replicate your problem, but you should be able to do everything you need to without selecting each chart.

Private Sub ComboBox1_Change()
    Dim chtObj As ChartObject
    Dim cht As Chart

    Application.ScreenUpdating = False
        For Each chtObj In ActiveSheet.ChartObjects
            Set cht = chtObj.Chart
            cht.ChartTitle.Text = ComboBox1.Value
            'Some other stuff, excluded.
        Next chtObj
    Application.ScreenUpdating = True
End Sub

Upvotes: 2

Related Questions