Results 1 to 4 of 4

Thread: [RESOLVED] How to validate existing shape in VBA?

  1. #1

    Thread Starter
    Member kryptonboy22's Avatar
    Join Date
    May 2006
    Location
    philippines
    Posts
    48

    Resolved [RESOLVED] How to validate existing shape in VBA?

    I have to pass a value to a shape in another sheet in excel but i get an error when the shape does not exist. im also passing the shape name to a string that's why there will be a possibility that the said shape would not exist. i have to define the shape and its value. so please can anyone help me how to validate if the said shape is existing or not.

    here's my code but i get an error when its false

    VB Code:
    1. If Sheet4.Shapes(gCell).ControlFormat.Enabled = True Then
    2.        Sheet4.Shapes(gCell).TextFrame.Characters.Text = valCel
    3. Else
    4.        Sheet4.Range(gCell).Value = valCel
    5. End If

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

    Re: How to validate existing shape in VBA?

    Moved
    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
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to validate existing shape in VBA?

    you have to run through the shapes collection
    VB Code:
    1. Sub shapes()
    2. Dim s As Shape
    3. For Each Shape In Sheet4.shapes 'use whatever sheet reference
    4.      If s.Name = gcell Then
    5.      MsgBox "shape exists"
    6.      Exit For
    7. Next
    8. End Sub
    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

  4. #4

    Thread Starter
    Member kryptonboy22's Avatar
    Join Date
    May 2006
    Location
    philippines
    Posts
    48

    Re: [RESOLVED] How to validate existing shape in VBA?

    thanks westconn1 it works although i changed your code for while shape as s to make it run. And RobDog888 sorry for wrong posting, thanks for moving it here!

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