+ Reply to Thread
Results 1 to 9 of 9

Excel 2007 : Macro to keep columns by specific column heading names

  1. #1
    Registered User
    Join Date
    03-08-2012
    Location
    Idaho
    MS-Off Ver
    Excel 2007
    Posts
    10

    Macro to keep columns by specific column heading names

    I pull an excel report frequently that has approx 200 columns - I only need about 100 of them. How do I create a macro to keep only the columns I need by specific column heading name?

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,614

    Re: Macro to keep columns by specific column heading names

    What is/are the criteria for hiding the columns you don't want/need?

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    03-08-2012
    Location
    Idaho
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macro to keep columns by specific column heading names

    I simply do not need/use them.

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Macro to keep columns by specific column heading names

    What TMS means is how can we write a macro without knowing which ones are supposed to be hidden. Please upload a copy of your workbook and you can dummy up the data but the structure should remain the same. Also we need to know which columns you want to keep or which you want to hide.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  5. #5
    Registered User
    Join Date
    03-08-2012
    Location
    Idaho
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macro to keep columns by specific column heading names

    This was solved for me by WINKO - THANKS!!:

    Sub Keep_specific_columns1()
    '
    ' DeleteColumnsOnSheet Macro
    ' Developer: Winko Erades van den Berg
    ' E-mail : winko at winko-erades.nl
    ' Developed: 09-03-2012
    ' Modified: 09-03-2012
    ' Version: 1.0
    '

    Dim iCol As Long

    'Constant values
    data_sheet1 = InputBox("TEST1:") 'Create Input Box to ask the user which sheet needs to be reorganised

    'Start with counting columns
    For iCol = 1 To Sheets(data_sheet1).UsedRange.Columns.Count

    'Read the header of the sheet to determine whether the column needs to be deleted
    TargetCol = 0
    If Sheets(data_sheet1).Cells(1, iCol).Value = "L72A" Then TargetCol = 1
    If Sheets(data_sheet1).Cells(1, iCol).Value = "L73A" Then TargetCol = 1
    If Sheets(data_sheet1).Cells(1, iCol).Value = "L74A" Then TargetCol = 1

    'If a column doesn't match then delete column
    If TargetCol = 0 Then
    Sheets(data_sheet1).Columns(iCol).Delete
    If iCol < Sheets(data_sheet1).UsedRange.Columns.Count Then
    iCol = iCol - 1
    Else
    End If
    End If

    Next iCol 'Move to the next column until all columns are read

    End Sub

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,614

    Re: Macro to keep columns by specific column heading names

    I'm glad you have a solution and you have shared it with us. He clearly has some infomation or insight that we do not have.

    As Winko appears not to be a member of this forum, I suspect that you have cross posted without making us aware of that fact.

    That implies that your post does not comply with Rule 8 of our Forum RULES. Cross-posting is when you post the same question in other forums on the web. You'll find people are disinclined to respond to cross-posts because they may be wasting their time solving a problem that has been solved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser)to the cross-post. Expect cross-posts without a link to be closed a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).


    And ...

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here


    Regards, TMS

  7. #7
    Registered User
    Join Date
    03-08-2012
    Location
    Idaho
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macro to keep columns by specific column heading names

    I did not cross-post. I found WINKO on this Forum. ??

  8. #8
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Macro to keep columns by specific column heading names

    TMS means that any communications regarding possible solutions should be made publicly in this thread. If WINKO provided you the solution via a private channel then this is against the forum rules. Since there are no posts in this thread from anyone named WINKO therefore the natural assumption is that the OP cross-posted (which happens more often that you can imagine). It's frustrating to spend time working out a solution to only realize that the answer was already provided by someone else on another forum. That drains the energy and time that we have to help people. I hope you understand and will clarify the sources of your solutions in the future.

    Good luck.

    abousetta

    P.S. If you have found an adequate solution then please mark the thread as solved and add code tags around your posted code. Thanks.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,614

    Re: Macro to keep columns by specific column heading names

    No WINKO.

    Maybe one of these ....

    winiko wink Wink121 winkblink winklar winkle winkle10ca winklenugget Winkler1 winklerg winklew winkus


    Whatever, there's no input from him in the thread which rather defeats the purpose of an open forum.


    Regards, TMS

+ 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