Created
July 27, 2011 13:43
-
-
Save hoffstein/1109382 to your computer and use it in GitHub Desktop.
Excel find column by header text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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