Skip to content

Instantly share code, notes, and snippets.

@hoffstein
Created July 27, 2011 13:43
Show Gist options
  • Save hoffstein/1109382 to your computer and use it in GitHub Desktop.
Save hoffstein/1109382 to your computer and use it in GitHub Desktop.
Excel find column by header text
Function FindColumn(headerText As String, Optional sheetToSearch As Worksheet, Optional headerRow As Integer) As Integer
Dim foundCell As Range
If headerRow = 0 Then headerRow = 1
If sheetToSearch Is Nothing Then Set sheetToSearch = ActiveSheet
Set foundCell = sheetToSearch.Rows(headerRow).Find(what:=headerText, lookat:=xlWhole)
If Not foundCell Is Nothing Then
FindColumn = foundCell.Column
Else
Err.Raise Number:=vbObjectError + 1000, Source:="FindColumn", _
Description:="Column not found: headerText=""" & headerText & _
""", sheetToSearch=" & sheetToSearch.Name & ", headerRow=" & headerRow
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment