Checking to see if shape exists without error handling

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
So basically I am using the following code to see if a shape exists

Code:
On error Resume Next
If .Shapes("Input_Textbox") Is Nothing Then
   .Shapes.AddTextbox(msoTextOrientationHorizontal, DropDownBox.Left, DropDownBox.Top + Padding, InputBox_Wdith, InputBox_Height).Name = "Input_Textbox"
End If

I want to get around having to use On error because I figure there is a better way to check if something exists without it generating an error.
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Use the code below to make a new function for the check:

Code:
<code class="code-10 nolinks" id="code-899207">Function ShapeExists(shpName As String) as Boolean

    Dim myShapes As Shape
    Dim blnFound As Boolean
    
    blnFound = False
    
    For Each myShapes In ActiveSheet.Shapes
        If myShapes.Name = shpName Then
            blnFound = True
            <code class="code-10 nolinks" id="code-899207">Return blnFound</code>
        End If
    Next
    
    Return blnFound 
End Function
</code>

Now add a test before your code like this:

Code:
If ShapeExists("Input_Textbox") Then
 
Last edited:
Upvote 0
Use the code below to make a new function for the check:

Code:
<code class="code-10 nolinks" id="code-899207">Function ShapeExists(shpName As String) as Boolean

    Dim myShapes As Shape
    Dim blnFound As Boolean
    
    blnFound = False
    
    For Each myShapes In ActiveSheet.Shapes
        If myShapes.Name = shpName Then
            blnFound = True
            <code class="code-10 nolinks" id="code-899207">Return blnFound</code>
        End If
    Next
    
    Return blnFound 
End Function
</code>

Now add a test before your code like this:

Code:
If ShapeExists("Input_Textbox") Then

Thanks for the solution. Return draws an error so I removed it from the if then statement and just put set ShapeExists = blnFound at the bottom but it draws an object required error on this function. Not sure why but I see your logic and i like it.
 
Upvote 0
Great! "Return" might be VB.NET and I get confused sometimes. Glad you got it going.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,432
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top