通过 VBA 在 excel 中使形状不可见/可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33573566/
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
Making shapes invisible/visible in excel through VBA
提问by Matthew Brophy
I'm having this problem for the last few hours and I would really appreciate some help with it.
在过去的几个小时里我遇到了这个问题,我真的很感激能得到一些帮助。
Basically, I want to be able to hide/unhide shapes depending on selections a user makes on a userform. I've broken the problem down into a very simple example. If I insert a shape called "oval 1" in a sheet and run the code:
基本上,我希望能够根据用户在用户表单上所做的选择来隐藏/取消隐藏形状。我已将问题分解为一个非常简单的示例。如果我在工作表中插入一个名为“椭圆 1”的形状并运行代码:
Sub hideshape()
With ActiveSheet
.Shapes("Oval 1").Select
With Selection
.Visible = False
End With
End With
End Sub
the shape disappears but when I run this code
形状消失了,但是当我运行此代码时
Sub unhideshape()
With ActiveSheet
.Shapes("Oval 1").Select
With Selection
.Visible = True
End With
End With
End Sub
I get an error "Requested Shapes are locked for Selection"
我收到错误“请求的形状已被锁定以供选择”
The workbook is not protected and I have tried un-ticking locked and locked text on the shape properties.
工作簿不受保护,我尝试取消勾选形状属性上的锁定和锁定文本。
Any ideas what's causing this.
任何想法是什么导致了这种情况。
回答by A.S.H
You cannot Selecta hidden object. However, you dont need to use Selectat all, and it is usually not recommended. Try simply:
你不能Select成为一个隐藏的对象。但是,您Select根本不需要使用,通常不推荐使用。简单地尝试:
Sub HideShape()
ActiveSheet.Shapes("Oval 1").Visible = False
End Sub
Sub UnhideShape()
ActiveSheet.Shapes("Oval 1").Visible = True
End Sub
回答by Armand Villa
Sub HideEachShape()
Dim sObject As Shape
For Each sObject In ActiveSheet.Shapes
sObject.Visible = False
Next
End Sub
from: extendoffice.com
回答by tpjmlu26
I hide shapes based on their name since some shapes I don't want to hide. I use this format:
Sheet1.Shapes.Range(Array("COtxtBox1")).Visible = msoTrue
我根据名称隐藏形状,因为有些形状我不想隐藏。我使用这种格式:
Sheet1.Shapes.Range(Array("COtxtBox1")).Visible = msoTrue
name of your shape or shapes goes into the array
您的一个或多个形状的名称进入数组
if it only 1 shape you could just use:
如果它只有一种形状,你可以使用:
Sheet1.Shapes.range("COtxtBox1").Visible = True
Sheet1.Shapes.range("COtxtBox1").Visible = True
I found that the "mso" part is not necessary for the True or False statement
我发现 True 或 False 语句不需要“mso”部分
回答by TiIiMuRkA
I solved problem with this code(Oval = Type 9, from MsoAutoShapeType Enumeration (Office)):
我用这个代码解决了问题(Oval = Type 9,来自MsoAutoShapeType Enumeration (Office)):
Sub hide()
s = ActiveSheet.Shapes.Count
For i = 1 To s
If ActiveSheet.Shapes(i).Type = 9 Then ActiveSheet.Shapes(i).Visible = False
Next i
End Sub
Sub unhide()
s = ActiveSheet.Shapes.Count
For i = 1 To s
If ActiveSheet.Shapes(i).Type = 9 Then ActiveSheet.Shapes(i).Visible = True
Next i
End Sub
If "Type = 9" is wrong, you can find out type of your shape with code in Immediatewindow (ctrl+G in VBA):
如果“Type = 9”是错误的,你可以在立即窗口中用代码找出你的形状的类型(VBA中的ctrl+G):
?ActiveSheet.Shapes("Oval 1").Type
回答by Muhammad Kafiansyah
Public HIDE As Boolean
Sub T_BUTTON ()
ActiveSheet.Shapes("T 1").Visible = HIDE
If ActiveSheet.Shapes("T 1").Visible = False Then
HIDE = True
Else
HIDE = False
End If
END SUB

