How to unselect or deselect an object.

canes

New Member
Joined
Jun 16, 2004
Messages
7
Suppose a graphic obect on a worksheet is selected (A) by VBA or (II) by the user who, say, gets tossed by a Chart event. See code below.
Question is: How to cleanly deselect or unselect or selectnotwantnot the item (don't want selection handles no more) through VBA.
  • DO NOT want to select some arbitrary cell A1 or other address.
    DO NOT want to select some other off page or hidden object.
    DO NOT NOT want to issue Sendkeys {ESC}, False.
    DO NOT NOT not want to issue Sendkeys {ESC}, True.
Want code to produce equivalent of .Deselect or .Unselect if such a thing existed.

'Example (A) of code that does a select
Code:
Sub selchart() 
    ActiveSheet.ChartObjects(1).Select
End Sub
'Example (II) of code that traps a select
Code:
Private Sub myChartClass_Select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long)
    Select Case ElementID  
         Case xlChartTitle: MsgBox "Futz do not the chart title with."
    End Select
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Well, that's the error text that Help says. The Error dialog says "Method 'Deselect' of Object '_Chart' failed (Run time error 1004)
 
Upvote 0
Another thang - the object doesn't have to be a chart - it could be a group, a control, et c.
Thanks.
 
Upvote 0
Haven't tried it, but what happens if you try to select a non-existent object with on error resume next -- is the original object still selected?
 
Upvote 0
canes said:
Well, that's the error text that Help says. The Error dialog says "Method 'Deselect' of Object '_Chart' failed (Run time error 1004)

What version of Excel are you using? It worked when I tried it.

Another possibilty is to Select the Range that was selected immediately before the Chart was activated:

Code:
Dim Win As Window
For Each Win In ThisWorkbook.Windows
    If Win.Index <> ActiveWindow.Index Then
        Win.RangeSelection.Select
        Exit For
    End If
Next Win

Note that the RangeSelection property is new in Excel 2000.
 
Upvote 0
Yesssssss. Getting there.
The RangeSelection thing works. (Just had to change the <> to =)
However, only get it to work if the sheet containing the selected object is the active sheet.
If I have
Worksheets("Sheet1").Shapes("myshape").Select
and the active sheet is Sheet2, then the RangeSelection doesn't happen in sheet1. neither does it happen if I loop throuh each sht in Sheets.

BTW I am running Windows XP and Excel in Office 2003
 
Upvote 0
RangeSelection is a property of the Windows object, not of the Sheet object, so looping through the Sheets won't help.

If all your selecting is in code, it shouldn't be difficult to save the Selection at the start and Select it again at the end. And, in any case, selecting in code is rarely necessary.

So what are you really trying to achieve?
 
Upvote 0
Why do I want to do that?
Here are a couple or 3 examples of why an item is selected and thereafter the item should be deselected.
  • Example a: Inserting a picture leaves the object selected
    Example 2: Superimposing n images and bringing a desired one to the front requires object to be selected.
    Example III: See Example II in original post.
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top