Is there a simple way to select a column based on a text string in the header row?
Thanks,
Jim
Is there a simple way to select a column based on a text string in the header row?
Thanks,
Jim
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!
Do you mean locate the text in the header and then select the column where you find the text?
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.
Use this code considering that your header is in row 1.
The output will come as a number.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
This macro will find the cell header you input and select the entire column. If not found, it will tell you so.
Hope that helps!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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks