View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
SteAXA SteAXA is offline
external usenet poster
 
Posts: 22
Default A generic "find" function

This function find the first cell (from top or from bottom), return the name
of cell and stop.
If you need to go to the next or preview cell you must change the function.

Function ToSearchFor(ByVal sToFind As String, ByVal sColumn As String, ByVal
sFrom As String)
If sToFind = "" And sColumn = "" And Not (sFrom = "T" Or sFrom = "B") Then
Exit Function
End If

Dim nRow As Integer
Dim bStop As Boolean
Dim sTest As String

Range(sColumn & "1").Select
If sFrom = "T" Then
nRow = 1
Else
Selection.End(xlDown).Select
nRow = ActiveCell.Row
End If
While Not bStop
sTest = ActiveCell.Value
If sTest = "" Then
MsgBox "Text not finded in column " & sColumn
bStop = True
Else
If InStr(1, sTest, sToFind, vbTextCompare) 0 Then
MsgBox "Text finded in cell " & sColumn & nRow
bStop = True
End If
End If
If Not bStop Then
If sFrom = "T" Then
ActiveCell.Offset(1, 0).Select
Else
ActiveCell.Offset(-1, 0).Select
End If
End If
If sFrom = "T" Then
nRow = nRow + 1
Else
nRow = nRow - 1
End If
Wend

End Function

You can call the function draw a userform or like this:

Sub SearchFor()
Dim sToFind As String
Dim sColumn As String
Dim sFrom As String

sToFind = InputBox("String to find")
sColumn = InputBox("Column to search in")
sFrom = InputBox("For start to top type T, to bottom B, E for exit
function")

ToSearchFor sToFind, sColumn, sFrom

End Sub

Good work!