选择单元格中的所有形状

时间:2018-05-25 09:49:12

标签: vba excel-vba excel

我需要选择给定单元格中的所有形状。 我写了这段代码,但它产生了错误。

Dim sh as shape
For Each sh In ActiveSheet.Shapes
If Not Intersect(Range("B2"), sh.TopLeftCell) Is Nothing Then
    sh.SelectAll   'Error
End If
Next sh

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

SelectAll将选择工作表上的所有形状。要做到这一点,你可以使用:ActiveSheet.Shapes.SelectAll

要选择特定形状,您需要选择它们而不替换之前的选择。

Shape.Select有一个可选参数Replace

  

(仅用于床单)。如果为True,则替换当前选择   指定的对象。 False将当前选择扩展为包含   任何先前选择的对象和指定的对象。

如果它们位于单元格B2中,您需要做的就是遍历形状并将它们添加到选区中。

Sub Test()

    Dim sh As Shape

    For Each sh In ActiveSheet.Shapes
        If Not Intersect(Range("B2"), sh.TopLeftCell) Is Nothing Then
            sh.Select False
        End If
    Next sh

End Sub
相关问题