+ Reply to Thread
Results 1 to 5 of 5

Find Column heading and then Delete entire column

  1. #1
    Registered User
    Join Date
    09-18-2003
    Location
    London
    Posts
    15

    Red face Find Column heading and then Delete entire column

    Hi,

    I am trying to find a column heading within a worksheet and then delete the entirecolumn. The following line of code does this fine but the column I am trying to 'find' will not always be present. When the column is not present this line of code will debug and I would like to write something better than an 'On error' statement to proceed with the code. I've been trying to put it in some kind of IF statement but without success:

    .Rows(1).Find("Myvalue").EntireColumn.Delete

    I'm sure this is pretty simple but I'm having one of those days. Any help, as always, would be much appreciated.

    Thanks,

    Adrian

  2. #2
    Dave Peterson
    Guest

    Re: Find Column heading and then Delete entire column

    dim FoundCell as range
    with activesheet.rows(1)
    set foundcell = .cells.find(what:="myValue",.....)
    end with

    if foundcell is nothing then
    'not found
    else
    foundcell.entirecolumn.delete
    end if



    Kobayashi wrote:
    >
    > Hi,
    >
    > I am trying to find a column heading within a worksheet and then delete
    > the entirecolumn. The following line of code does this fine but the
    > column I am trying to 'find' will not always be present. When the
    > column is not present this line of code will debug and I would like to
    > write something better than an 'On error' statement to proceed with the
    > code. I've been trying to put it in some kind of IF statement but
    > without success:
    >
    > Rows(1).Find("Myvalue").EntireColumn.Delete
    >
    > I'm sure this is pretty simple but I'm having one of those days. Any
    > help, as always, would be much appreciated.
    >
    > Thanks,
    >
    > Adrian
    >
    > --
    > Kobayashi
    > ------------------------------------------------------------------------
    > Kobayashi's Profile: http://www.excelforum.com/member.php...nfo&userid=871
    > View this thread: http://www.excelforum.com/showthread...hreadid=476777


    --

    Dave Peterson

  3. #3
    Registered User
    Join Date
    09-18-2003
    Location
    London
    Posts
    15

    Thumbs up

    Dave,

    Many thanks indeed! Works a treat, although I didn't want the code to execute anything should the value not be found so I took the liberty of amending your code to:

    If FoundCell Is Nothing Then
    'not found
    Else
    FoundCell.EntireColumn.Delete
    End If

    to

    If Not FoundCell Is Nothing Then
    FoundCell.EntireColumn.Delete
    End If

    Many thanks again!

    Best Regards,

    Adrian

  4. #4
    Registered User
    Join Date
    09-18-2003
    Location
    London
    Posts
    15
    Forgot to add rating.

  5. #5
    Dave Peterson
    Guest

    Re: Find Column heading and then Delete entire column

    I bet if you try both versions, you won't notice a difference.



    Kobayashi wrote:
    >
    > Dave,
    >
    > Many thanks indeed! Works a treat, although I didn't want the code to
    > execute anything should the value not be found so I took the liberty of
    > amending your code to:
    >
    > If FoundCell Is Nothing Then
    > 'not found
    > Else
    > FoundCell.EntireColumn.Delete
    > End If
    >
    > to
    >
    > If Not FoundCell Is Nothing Then
    > FoundCell.EntireColumn.Delete
    > End If
    >
    > Many thanks again!
    >
    > Best Regards,
    >
    > Adrian
    >
    > --
    > Kobayashi
    > ------------------------------------------------------------------------
    > Kobayashi's Profile: http://www.excelforum.com/member.php...nfo&userid=871
    > View this thread: http://www.excelforum.com/showthread...hreadid=476777


    --

    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