你们中有人知道一种方法可以避免循环遍历工作表的形状库存 - 或者 - 循环遍历包含形状名称的范围吗?我试图避免整体循环,并在想是否有某种方法可以大量收集所有这些并执行 Bulk.Visible = False。
感谢您对这个主题感兴趣!
我已经尝试使用形状定义数组或连接/联合来尝试获取它们的聚合,以便一次性设置 .Visible 设置。
最简单的方法是@Tim Williams 在第一条评论中建议的方法,所以功劳应该归于他:
Dim sh As Worksheet: Set sh = ActiveSheet
sh.DrawingObjects.Visible = msoFalse
sh.DrawingObjects.Visible = msoTrue
但是这种方式使用了一个没有详细记录的属性,甚至
IntelliSense
也没有显示它。
这就是为什么下一个方法可以被认为更好,能够看到你还可以用该对象做什么:
Dim shRng As ShapeRange
Set shRng = sh.DrawingObjects.ShapeRange
'followed by
shRng.Visible = msoFalse
shRng.Visible = msoTrue
另一种方法使用工作表的
Range
属性 Shapes
一个:
Dim arrSh()
arrSh = Evaluate("TRANSPOSE(ROW(1:" & sh.Shapes.count & "))") 'all shapes indexes in an array
sh.Shapes.Range(arrSh).Visible = msoFalse
sh.Shapes.Range(arrSh).Visible = msoTrue
sh.Shapes.Range(Array(1,3)).Visible = msoTrue 'makes visible the first and the third shape
您可以使用这样的过滤数组(通过迭代获得,或直接从工作表中获得),然后将其用作
Range
数组:
Dim arrN(),i As Long, k as Long
arrN = Application.Transpose(sh.Range("A2:A10").Value2). Supposing that the shapes name to be hiden - unhidden are in "A2:A10" range
'or obtain it by iteration according to shapes type:
ReDim arrN(1 To sh.Shapes.count)
k = 1
For i = 1 To sh.Shapes.count
Select Case sh.Shapes(i).Type
Case msoFormControl, msoOLEControlObject
'do nothing in case of Form or ActiveX controls
Case Else
arrN(k) = sh.Shapes(i).name 'colect control names in the array to be handled
k = k + 1
End Select
Next i
ReDim Preserve arrN(k - 1) 'remove array empty elements
sh.Shapes.Range(arrN).Visible = msoFalse
sh.Shapes.Range(arrN).Visible = msoTrue
您还可以将形状索引保存在工作表中,以便不必每次都确定它,并从如上所示的范围中提取它。
如果有不清楚的地方,请随时要求澄清。