如何根据范围选择多个形状?

时间:2015-07-03 18:24:57

标签: excel vba excel-vba

如果所选范围由1个单元格组成,则选择工作表中的所有形状,否则选择范围中的形状。这是给我带来麻烦的“别的”部分。我可以选择一个形状,但不是所有形状......

Public Sub ShapeSelection()
Dim Sh As Shape
On Error Resume Next

If Selection.Rows.count * Selection.Columns.count = 1 Then
    ActiveSheet.Shapes.SelectAll
Else
    Application.ScreenUpdating = False
    With ActiveSheet
       For Each Sh In .Shapes
           If Not Application.Intersect(Sh.TopLeftCell, .Range(Selection.Address)) Is Nothing Then
              Sh.Select
           End If
        Next Sh
    End With
    Application.ScreenUpdating = True
End If

End Sub

1 个答案:

答案 0 :(得分:1)

试试这个。请注意包含“False”一词:

Public Sub ShapeSelection()
Dim Sh As Shape
Dim selectedOne As Boolean
On Error Resume Next

If Selection.Rows.count * Selection.Columns.count = 1 Then
    ActiveSheet.Shapes.SelectAll
Else
    Application.ScreenUpdating = False
    With ActiveSheet
       For Each Sh In .Shapes
           If Not Application.Intersect(Sh.TopLeftCell, .Range(Selection.Address)) Is Nothing Then
              If selectedOne = False Then
                  Sh.Select
                  selectedOne = True
               Else
                  Sh.Select(False)
               End If
           End If
        Next Sh
    End With
    Application.ScreenUpdating = True
End If

End Sub