Skip to content

Instantly share code, notes, and snippets.

@Hunu
Last active May 20, 2020 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hunu/16b42e4f069a6a16766f98bd66e09562 to your computer and use it in GitHub Desktop.
Save Hunu/16b42e4f069a6a16766f98bd66e09562 to your computer and use it in GitHub Desktop.
This is a User Defined Formula in EXCEL, which will looking for the first cell that contains(NOT the 'match') the str_to_find in a column, and return it's number of row.
Function find_str_in_a_column(str_to_find As String, col_no As Integer, wws As Worksheet) As Integer
'In {wws}(working worksheet), column of {col_no},RETURN the row number of the first cell which contains {str_to_find}
'Or return -1 if not found
Dim arr()
Dim re As Integer
Dim i As Integer
With wws
re = .Cells(65536, col_no).End(xlUp).Row
arr = .Range(.Cells(1, col_no), .Cells(re, col_no)).Value2
End With
find_str_in_a_column = -1
For i = 1 To re
If InStr(1, arr(i, 1), str_to_find) > 0 Then
find_str_in_a_column = i
exit function
End If
next i
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment