+ Reply to Thread
Results 1 to 6 of 6

How to select column by header

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2011
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    17

    How to select column by header

    Is there a simple way to select a column based on a text string in the header row?

    Thanks,
    Jim

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: How to select column by header

    Hi Jim,

    What do you mean "select"? A specific text string can be searched for and found using formulas or VBA, but what are you trying to do?

    Thanks!

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to select column by header

    Do you mean locate the text in the header and then select the column where you find the text?

  4. #4
    Registered User
    Join Date
    10-24-2011
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: How to select column by header

    Sorry for not being clear. I want to find then select the entire colum that has a particular name in row 1. The column may not be in the same place (A to ZZ) in different workbooks, but it will always have the same header name.

    I want to select specific named columns to perform operations. It may be to copy to another worksheet or to delete based on the header name.

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to select column by header

    Use this code considering that your header is in row 1.
    Dim cellcol As Long
        
        Rows("1:1").Select
        Cells.Find(What:="Name of column", After:=ActiveCell, LookIn:=xlValues, Lookat:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, searchformat:=False).Activate
        cellcol = ActiveCell.Column
    The output will come as a number.

  6. #6
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: How to select column by header

    This macro will find the cell header you input and select the entire column. If not found, it will tell you so.
    Sub findcol()
    Dim ans As String
    ans = InputBox("Find column header:", "Column Header Search", "")
    If ans <> "" Then
        On Error GoTo notfound
        Range("1:1").Find(What:=ans, After:=Range("A1"), LookIn:=xlValues, Lookat:=xlWhole, _
            SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
            SearchFormat:=False).EntireColumn.Select
        On Error GoTo 0
    End If
    Exit Sub
    
    notfound:
    MsgBox "That entry was not found in the header row.  Please try again."
    End Sub
    Hope that helps!

+ 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