+ Reply to Thread
Results 1 to 7 of 7

Delete Columns with duplicate headers

  1. #1
    Forum Contributor
    Join Date
    01-06-2004
    Location
    Carbondale CO
    Posts
    245

    Delete Columns with duplicate headers

    Hi,
    I have a range of column headers ("K1:IP1") that I would like to loop through and find all duplicates. Then delete the entire column with the duplicate header. I would like to keep the original header and column.
    Example:
    K L M N O P..........................
    Stone CMU Brick Grout Stone Stone

    Columns O & P would be deleted but column K would remain.
    Casey

  2. #2
    Ardus Petus
    Guest

    Re: Delete Columns with duplicate headers

    Hi Casey

    Sub DelDupeCols()
    Dim lCol As Long
    Dim rFound As Range
    For lCol = Columns("P").Column To Columns("L").Column Step -1
    Set rFound = Range(Range("K1"), Cells(1, lCol - 1)).Find( _
    what:=Cells(1, lCol).Value, _
    LookIn:=xlValues, _
    lookat:=xlWhole)
    If Not rFound Is Nothing Then
    Columns(lCol).Delete
    End If
    Next lCol
    End Sub

    HTH
    --
    AP

    "Casey" <[email protected]> a écrit dans le
    message de news: [email protected]...
    >
    > Hi,
    > I have a range of column headers ("K1:IP1") that I would like to loop
    > through and find all duplicates. Then delete the entire column with the
    > duplicate header. I would like to keep the original header and column.
    > Example:
    > K L M N O
    > P..........................
    > Stone CMU Brick Grout Stone Stone
    >
    > Columns O & P would be deleted but column K would remain.
    >
    >
    > --
    > Casey
    >
    >
    > ------------------------------------------------------------------------
    > Casey's Profile:
    > http://www.excelforum.com/member.php...fo&userid=4545
    > View this thread: http://www.excelforum.com/showthread...hreadid=547921
    >




  3. #3
    Forum Contributor
    Join Date
    01-06-2004
    Location
    Carbondale CO
    Posts
    245
    Ardus,
    Perfect! I will need to study what makes this routine work. I am particularly befuddled by the Columns("P").Column To Columns("L") statement. Usually when I see a bit of code I can see the logic right away, but this has me scratching my head. Thank you very much for the reply. Good stuff.

  4. #4
    Tom Ogilvy
    Guest

    Re: Delete Columns with duplicate headers

    For lCol = Columns("P").Column To Columns("L").Column Step -1

    is the same as

    for lCol = 16 to 12 step -1

    confirming from the immediate window:

    ? columns("P").column
    16
    ? columns("L").column
    12



    --
    Regards,
    Tom Ogilvy


    "Ardus Petus" wrote:

    > Hi Casey
    >
    > Sub DelDupeCols()
    > Dim lCol As Long
    > Dim rFound As Range
    > For lCol = Columns("P").Column To Columns("L").Column Step -1
    > Set rFound = Range(Range("K1"), Cells(1, lCol - 1)).Find( _
    > what:=Cells(1, lCol).Value, _
    > LookIn:=xlValues, _
    > lookat:=xlWhole)
    > If Not rFound Is Nothing Then
    > Columns(lCol).Delete
    > End If
    > Next lCol
    > End Sub
    >
    > HTH
    > --
    > AP
    >
    > "Casey" <[email protected]> a écrit dans le
    > message de news: [email protected]...
    > >
    > > Hi,
    > > I have a range of column headers ("K1:IP1") that I would like to loop
    > > through and find all duplicates. Then delete the entire column with the
    > > duplicate header. I would like to keep the original header and column.
    > > Example:
    > > K L M N O
    > > P..........................
    > > Stone CMU Brick Grout Stone Stone
    > >
    > > Columns O & P would be deleted but column K would remain.
    > >
    > >
    > > --
    > > Casey
    > >
    > >
    > > ------------------------------------------------------------------------
    > > Casey's Profile:
    > > http://www.excelforum.com/member.php...fo&userid=4545
    > > View this thread: http://www.excelforum.com/showthread...hreadid=547921
    > >

    >
    >
    >


  5. #5
    Forum Contributor
    Join Date
    01-06-2004
    Location
    Carbondale CO
    Posts
    245
    Tom,
    Thanks for the explaination. I think what threw me was the Columns ("P") since my OP set the range to column IP. After I studied it a while I connected the dots. But I always appreciate the lessons from you.
    I tried to go bankrupt for 3 years teaching high school and I just had the light go on for using the immediate window. Good teachers do that for people. I have very little knowledge about how to use it effectively. I'm getting pretty good with watches, but I would love some training in using the Immediate window. Right now it is hit or miss, whether I get any useful insight from the Immediate window. I keep trying. Thanks again.

  6. #6
    Tom Ogilvy
    Guest

    Re: Delete Columns with duplicate headers

    Basically, you can execute almost any individual command/line of code that
    you can write in a module from the immediate window

    That is one way to use it. This way you can test what the code is going to
    do or use it to perform something on your sheet that is easier to code than
    to do manually.

    The other way to use it is to use debug.print statements in your actual
    code to monitor what is going on. Then your code flows without stopping or
    without altering the environment in which it is running. (many times you
    see people say they can step through their code, but it doesn't work when
    they run it).

    When your code errors and you go to debug, you can test variable values in
    the immediate window (or I assume you can use the watch window - I can't
    say, because I never use it - so I am not the best source for information on
    the watch window).



    --
    Regards,
    Tom Ogilvy



    "Casey" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Tom,
    > Thanks for the explaination. I think what threw me was the Columns
    > ("P") since my OP set the range to column IP. After I studied it a
    > while I connected the dots. But I always appreciate the lessons from
    > you.
    > I tried to go bankrupt for 3 years teaching high school and I just had
    > the light go on for using the immediate window. Good teachers do that
    > for people. I have very little knowledge about how to use it
    > effectively. I'm getting pretty good with watches, but I would love
    > some training in using the Immediate window. Right now it is hit or
    > miss, whether I get any useful insight from the Immediate window. I
    > keep trying. Thanks again.
    >
    >
    > --
    > Casey
    >
    >
    > ------------------------------------------------------------------------
    > Casey's Profile:

    http://www.excelforum.com/member.php...fo&userid=4545
    > View this thread: http://www.excelforum.com/showthread...hreadid=547921
    >




  7. #7
    Forum Contributor
    Join Date
    01-06-2004
    Location
    Carbondale CO
    Posts
    245
    Tom,
    Thanks for the immediate window lesson and on a Sunday no less.

+ 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