Results 1 to 8 of 8

Thread: [RESOLVED] Excel: select mult. shapes if their names contain a string

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    17

    Resolved [RESOLVED] Excel: select mult. shapes if their names contain a string

    Hi and thanks for reading my post!

    I have [any] number of shapes in one sheet. They have names such as "BigShape 10", "BigShape 11", "MedShape 7", etc.

    I need to select all shapes, whose name contain a certain string (for examle "gSh" from the example above).

    Could anybody kindly help, please?


    Many thanks in advance!!



    Pavel

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel: select mult. shapes if their names contain a string

    Welcome to the Forums.

    Are you needing this in Excel VBA or via VB 6 automation?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    17

    Re: Excel: select mult. shapes if their names contain a string

    Thanks for your reply. I am using VB 6.1 from Office XP.

  4. #4

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel: select mult. shapes if their names contain a string

    What type of shapes are they? All the same kind? How did you add them? From the Drawing toolbar, Controls toolbar, etc.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    17

    Re: Excel: select mult. shapes if their names contain a string

    They are all rectangles. I don't have the code with me at this moment, but I have written a macro that creates rectangles with appropriate size etc. then i have macro that changes selected shape according to its name. I just cannot figure out how to select several shapes (rectangles in my case) that have a certain string in their (custom) name.

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

    Re: Excel: select mult. shapes if their names contain a string

    VB Code:
    1. Sub selectshapes()
    2. Dim s As Shape, myarr(), n As Integer
    3. ReDim myarr(Sheet4.shapes.Count)
    4. For Each s In Sheet4.shapes
    5.     If Not InStr(s.Name, "Combo") = 0 Then
    6.          myarr(n) = s.Name
    7.          n = n + 1
    8.         End If
    9. Next
    10. ReDim Preserve myarr(n - 1)
    11.     Sheet4.shapes.Range(myarr).Select
    12.  
    13. End Sub
    this works for comboboxes, so should wor for your rectangles
    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

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    17

    Re: Excel: select mult. shapes if their names contain a string

    This is it! Many, many thanks!!
    I only had about 30 minutes to play with your code so far. It uses stuff I haven't use before, so I will read about it first. I also had to change sheet4 to ActiveSheet for some reason (renaming didn't work), but it already does what I needed so much. It selects the shapes with the string ("Combo" in your code). It seems like I will be using your code almost every day from now on. You really helped a lot! A lot!
    All the best wishes!!
    Pavel

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