+ Reply to Thread
Results 1 to 4 of 4

Check for existence

  1. #1
    CWillis
    Guest

    Check for existence

    I am pasting a text box using a macro. I would like for the macro to make
    sure that a text box doesn't already exist under the same name.
    If it does, delete it and then paste the new one.
    If it doesn't, paste the new one.
    Thanks in advance.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Response

    Try this...


    Sub Macro1()
    For Each Shape In ActiveSheet.Shapes
    If Shape.Name = "MyTextBoxName" Then
    Shape.delete
    End If
    Next Shape

    Insert your PASTE code here


    End Sub
    Martin

  3. #3
    Dave Peterson
    Guest

    Re: Check for existence

    You could check first, but if you're going to delete it, why not just delete it?

    'if the textbox is from the Drawing toolbar
    On Error Resume Next
    ActiveSheet.TextBoxes("text box 1").Delete
    On Error GoTo 0

    'if the textbox is from the control toolbox toolbar
    On Error Resume Next
    ActiveSheet.OLEObjects("textbox1").Delete
    On Error GoTo 0

    If the textbox isn't there to be deleted, the "on error resume next" line will
    tell excel to ignore the error.

    But you could just move/resize the existing one...



    CWillis wrote:
    >
    > I am pasting a text box using a macro. I would like for the macro to make
    > sure that a text box doesn't already exist under the same name.
    > If it does, delete it and then paste the new one.
    > If it doesn't, paste the new one.
    > Thanks in advance.


    --

    Dave Peterson

  4. #4
    CWillis
    Guest

    Re: Check for existence

    Thank you! Both will work. Plus some of that code will help with something
    I have later. Thanks again.

    "Dave Peterson" wrote:

    > You could check first, but if you're going to delete it, why not just delete it?
    >
    > 'if the textbox is from the Drawing toolbar
    > On Error Resume Next
    > ActiveSheet.TextBoxes("text box 1").Delete
    > On Error GoTo 0
    >
    > 'if the textbox is from the control toolbox toolbar
    > On Error Resume Next
    > ActiveSheet.OLEObjects("textbox1").Delete
    > On Error GoTo 0
    >
    > If the textbox isn't there to be deleted, the "on error resume next" line will
    > tell excel to ignore the error.
    >
    > But you could just move/resize the existing one...
    >
    >
    >
    > CWillis wrote:
    > >
    > > I am pasting a text box using a macro. I would like for the macro to make
    > > sure that a text box doesn't already exist under the same name.
    > > If it does, delete it and then paste the new one.
    > > If it doesn't, paste the new one.
    > > Thanks in advance.

    >
    > --
    >
    > Dave Peterson
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1