MS Excel 2003 - 处理形状时 Excel VBA 中的简单取消选择问题

发布于 2024-08-09 16:54:42 字数 552 浏览 2 评论 0原文

所以我有一个 Excel 工作簿,其中有一个全局映射方案。所以我在 Excel 中为每个国家/地区都有一个形状。根据区域选择,相对于数据/查询,它将以各种方式遮蔽区域/国家。

所以我知道如何在颜色、渐变阴影等方面操纵每个形状......

我不知道该怎么做是“取消选择”子例程末尾的形状。我的代码看起来像这样(非常简单):

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

好吧,从一个形状/国家/地区到另一个形状/国家/地区,“取消选择”并不是什么大不了的事情,因为焦点会跳跃,但最后???

我猜测/尝试了很多东西,但可惜没有运气,

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城仅有旧梦在 2024-08-16 16:54:42

你的源代码最后一行有一个拼写错误,...gradiend --> ...渐变

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

“取消选择”形状对象的一种非常简单的方法是添加这行代码,

sheet1.[A1].select

这会将焦点移动到工作表中的单元格 A1 ,从而远离您的对象。非常粗鲁,我不推荐这样做。我也不建议按照上面的建议“保存当前选择”,因为我们不知道光标是在单元格中还是在另一个(范围)对象中。

更好的方法是在整个脚本中完全避免“选择”。将您的形状分配给一个对象并操作该对象(注意:我在测试中使用第一个可用对象在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

更好的是,如果您正在处理一个为您提供形状名称的列表,请执行以下

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

操作这有帮助
祝你好运麦克D

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

多情出卖 2024-08-16 16:54:42

您不能简单地记录所选的原始单元格吗?

Dim oCell as Range
set oCell = activecell

'' Do stuff here

oCell.activate

更新:这一段代码记录了当前的选择,然后在选择范围(“A4”)后重新选择它。在不知道工作簿中有哪些形状的情况下,我无法验证这是否有效,但到目前为止我已经测试过了。

Set mySel = Application.Selection
[A4].Select
mySel.Select

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
笔落惊风雨 2024-08-16 16:54:42

我遇到了类似的问题,我需要在单击形状后隐藏它。我的解决方案是使用 SendKeys 来转义选择

SendKeys "{Esc}"

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 "{Esc}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文