How to select multiple shapes based on range?

我是研究僧i 提交于 2020-01-05 16:50:06

问题


If the selected range is composed of 1 cell then select all shapes in the sheet, else select the shapes in the range. It's the "else" part that's giving me trouble. I can select one shape, but not all shapes in the range...

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:


Try this. Note the inclusion of the word "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


来源:https://stackoverflow.com/questions/31212480/how-to-select-multiple-shapes-based-on-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!