views:

367

answers:

2

So I have an excel workbook that has a global mapping scheme in it. So I have a shape for each and every country in excel. Depending on the region select, relative to the data/query, it will shade regions/countries in various ways.

So I know how to manipulate each shape in terms of colors, gradient shading, etc....

What I don't know how to do is "unselect" the shape at the end of the sub routine. My code looks like this (real simple)

sheet1.shapes("CountryName").select selection.shaperange.fill.solid selection.shaperange.fill.visible = true selection.shaperange.fill.forecolor.rgb=rgb(110,110,110) selection.shaperange.fill.onecolorgradiend msogradienthorizontal, 2, 0.45

ok so from one shape/country/region to another the "unselect" is not that big of a deal because focus jumps, but at the end????

i have guessed/tried a bunch of stuff but alas no luck

thanks!

+2  A: 

Can you not simply record the original cell which was selected?

Dim oCell as Range
set oCell = activecell

'' Do stuff here

oCell.activate

Update: This bit of code records the current selection, then re-selects it after selecting range("A4"). Without knowing what kinds of shapes you have in your workbook, I can't verify that this will work, but it has so far with what I've tested.

Set mySel = Application.Selection
[A4].Select
mySel.Select
JakeTheSnake
what if the current selection is not a cell range but another shape object? in this case the selection moves to the cell that was selected BEFORE the selection moved to the recent shape object
MikeD
What shape object are you referring to? I created a text label, selected it, then ran this code: Set mySel = Application.Selection [A4].Select mySel.Select
JakeTheSnake
afaiu(nderstand) justin has a lot of shapes in one of his sheets, he could have selected one of these shapes instead of a cell when he fires his coloring procedure, so we cannot asume it is always a cell which is selected. your code would throw him back to the last cell selected before one of the shapes on his sheets was selected
MikeD
I realize my 1st answer did, but what about the comment I just made? That's different. (Edited original answer to include update)
JakeTheSnake
+2  A: 

you have a typo in your source last line, ...gradiend --> ...gradient

selection.shaperange.fill.onecolorgradienT msogradienthorizontal, 2, 0.45

a very simple way of "de-selecting" the shape object would be to add this line of code

sheet1.[A1].select

this moves the focus to cell A1 in your sheet and hence away fro your object. Very rude, and I don't recommend it. I also don't recommend to "save the current selection" as proposed above as we don't know if the cursor is in a cell or at another (range) object.

The bettwer way is to avoid "selection" completely throughout your script. Asign your shape to an object and manipulate that object (note: I simulated in sheet3 with the first available object in my test), i.e.

Sub test()
Dim MyShape As Shape
    Set MyShape = Sheet3.Shapes(1) ' or whatever shape according to the user input
    With MyShape.Fill
        .Solid
        .Visible = True
        .ForeColor.RGB = RGB(110, 110, 110)
        .OneColorGradient msoGradientHorizontal, 2, 0.45
    End With
End Sub

Even better, if you are processing a list that gives you the name of a shape, do the following

Sub Test()
    '
    ' get the shape's name into ShapeName
    ' ...

    ColorShape Sheet3.Shapes(ShapeName)

    ' ...

End Sub

Sub ColorShape(MyShape As Shape)
    With MyShape.Fill
        .Solid
        .Visible = True
        .ForeColor.RGB = RGB(110, 110, 110)
        .OneColorGradient msoGradientHorizontal, 2, 0.45
    End With
End Sub

Hope this helps Good luck MikeD

MikeD
sweet! this will help a lot. so more questions then: how do I assign the shape as an object?? is it already an object because it's been named? so the last example is something I was already thinking about doing function/subs being called by procedure based on what the relative procedure calling it is. wow this explains a lot to me though, because my little code looks soooo redundant compared to this!!! thanks! :D
Justin
assign shape to object:1st example: explicitely in asigning the named shape from the "List of Shapes" .... Set Set MyShape = Sheet3.Shapes(1) ... instead of numerical index you can reference by name in a string variable as well2nd example: implicitely by Sub Test() when handing over the object named "ShapeName" from the list of shapes called "Shapes()" towards the Sub ColorShape.it's always the same mechanism: objects are arranged in lists and you reference them by an index or a name. that works for shapes, worksheets, cell ranges, etc etc.hope that helps. Good luck MikeD
MikeD