I coded a function in a module that searches column A:A for a string (passed as an input) and returns the full content of the first cell in range A:A that contains that piece of string...
THe function works fine when I execute is from the sub test(), but I cannot make it work in the sheet directly... ie by writing =Find_First("lala"). It just returns "not found" although the piece of text is indeed in A:A
Here's my code.. Thanks for your help!!
<code>Public Function Find_First(FindString As String) As String
Dim Rng As Range
Dim TrimString As String
Application.Volatile True
TrimString = Trim(FindString)
If TrimString <> "" Then
With Sheets("Sheet1").Range("A:A")
Set Rng = .Find(What:=TrimString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Find_First = Rng.Value
MsgBox ("Found at: " & Rng.Address)
Else
Find_First = "not found"
MsgBox ("Not found ")
End If
End With
End If
End Function
Sub test()
MsgBox Find_First(" lala ")
End Sub</code>
Another thing to mention is that it seems to work on certain other computers... Maybe it is related to some settings?? I use Excel 14.5.4 (Office 2011 for MAC)...
THe function works fine when I execute is from the sub test(), but I cannot make it work in the sheet directly... ie by writing =Find_First("lala"). It just returns "not found" although the piece of text is indeed in A:A
Here's my code.. Thanks for your help!!
<code>Public Function Find_First(FindString As String) As String
Dim Rng As Range
Dim TrimString As String
Application.Volatile True
TrimString = Trim(FindString)
If TrimString <> "" Then
With Sheets("Sheet1").Range("A:A")
Set Rng = .Find(What:=TrimString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Find_First = Rng.Value
MsgBox ("Found at: " & Rng.Address)
Else
Find_First = "not found"
MsgBox ("Not found ")
End If
End With
End If
End Function
Sub test()
MsgBox Find_First(" lala ")
End Sub</code>
Another thing to mention is that it seems to work on certain other computers... Maybe it is related to some settings?? I use Excel 14.5.4 (Office 2011 for MAC)...