MS Excel 2003 - 处理形状时 Excel VBA 中的简单取消选择问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1671426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MS Excel 2003 - Simple unselect question in Excel VBA when dealing with shapes
提问by Justin
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.
所以我有一个 excel 工作簿,里面有一个全局映射方案。所以我对每个国家的excel都有一个形状。根据区域选择,相对于数据/查询,它将以各种方式遮蔽区域/国家。
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
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!
谢谢!
回答by MikeD
you have a typo in your source last line, ...gradiend --> ...gradient
你的源代码最后一行有一个错字,...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.
这会将焦点移至工作表中的单元格 A1,从而远离您的对象。非常粗鲁,我不推荐它。我也不建议像上面建议的那样“保存当前选择”,因为我们不知道光标是在单元格中还是在另一个(范围)对象上。
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.
更好的方法是在整个脚本中完全避免“选择”。将您的形状分配给一个对象并操作该对象(注意:我在 sheet3 中使用测试中的第一个可用对象进行了模拟),即
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
回答by SnakeWasTheNameTheyGaveMe
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.
更新:这段代码记录了当前选择,然后在选择范围(“A4”)后重新选择它。在不知道您的工作簿中有哪些形状的情况下,我无法验证这是否有效,但到目前为止我已经测试过了。
Set mySel = Application.Selection
[A4].Select
mySel.Select
回答by TinMan
I ran into a similar problem where I needed to hide a shape after it was click. My solution was to use SendKeys
to escape the selection
我遇到了一个类似的问题,我需要在点击后隐藏一个形状。我的解决方案是SendKeys
用来逃避选择
SendKeys "{Esc}"