Public Function FindHeader(HeaderText As String, Optional ourRow As Integer = 1) FindHeader = Rows(ourRow).Find(What:=HeaderText, _ LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _ SearchDirection:=xlNext, MatchCase:=False, _ SearchFormat:=False).Column End Function Public Function ColumnLetter(ColumnNumber As Integer) As String Dim n As Integer Dim c As Byte Dim s As String n = ColumnNumber Do c = ((n - 1) Mod 26) s = Chr(c + 65) & s n = (n - c) \ 26 Loop While n > 0 ColumnLetter = s End Function Sub testing() MsgBox (ColumnLetter(FindHeader("Search Text"))) End Sub
Friday, December 27, 2013
VBA - Returning the column letter for a given header in the first row
Using a wrapper for the find function - I prefer to use a wrapper to simplify my syntax elsewhere in the program - we will be able to return the numerical value for the column that has the header text we are looking for. We then use a function to convert this numerical value to the desired letter range:
Subscribe to:
Post Comments (Atom)
If you're feeling daring, you could also use the following formula (with some variation):
ReplyDelete=ADDRESS(ROW(INDEX(1:1,MATCH("column-name",1:1,0))),1)