Results 1 to 5 of 5

Thread: Selecting all shapes in a range in Excel (VBA)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2015
    Posts
    5

    Selecting all shapes in a range in Excel (VBA)

    I need all shapes within certain columns to dissapear/apear upon the click of a button. (Because shapes can be moved to other columns I do not want to specify the shapes in my code)

    Hiding/showing all shapes on the sheet I can do (cf. code below), but not for a specific range.

    Dim Sh as Shape

    With ActiveSheet
    For Each Sh In ActiveSheet.Shapes
    If Sh.Type = msoGroup Then 'only grouped shapes need to appear/dissapear
    Sh.Visible = msoTrue '/msoFalse
    End If
    Next Sh
    End With

    I've searched all over google for possible solutions, but don't find anything :-/

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Selecting all shapes in a range in Excel (VBA)

    you can try like
    Code:
    Set s = ActiveSheet
    On Error Resume Next
    For Each Sh In s.Shapes
        If Sh.GroupItems.Count > 1 Then
        If Err.Number = 0 Then Sh.Visible = Not Sh.Visible
        Err.Clear
        End If
    Next
    on error goto 0
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2015
    Posts
    5

    Re: Selecting all shapes in a range in Excel (VBA)

    Quote Originally Posted by westconn1 View Post
    you can try like
    Code:
    Set s = ActiveSheet
    On Error Resume Next
    For Each Sh In s.Shapes
        If Sh.GroupItems.Count > 1 Then
        If Err.Number = 0 Then Sh.Visible = Not Sh.Visible
        Err.Clear
        End If
    Next
    on error goto 0
    But how to do this for only shapes in specific columns? Let's say, shapes located in E:G?

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Selecting all shapes in a range in Excel (VBA)

    I think this would work:

    Code:
    If sh.TopLeftCell.Column > 4 And sh.TopLeftCell.Column < 8 Then
                'do something
            End If

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2015
    Posts
    5

    Re: Selecting all shapes in a range in Excel (VBA)

    Quote Originally Posted by vbfbryce View Post
    I think this would work:

    Code:
    If sh.TopLeftCell.Column > 4 And sh.TopLeftCell.Column < 8 Then
                'do something
            End If
    Super, that worked!
    Thanks a lot!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width