Hi there,
In one of my macros, there a part which I need several times. Until now, this part is copy pasted in the code everytime it is needed. This is not very elegant. Here the code we talk about, it's about looking up specific words in an Excel sheet and spitting out in which column they are:
' Variables
Dim Words As Variant
Dim WordsRange As Excel.Range
Dim i As Integer
' define search terms
Words = Array("Input 1", "Input 2", "Input 3")
' Look for each input
For i = LBound(Words) To UBound(Words)
Dim WordsRange As Excel.Range
Set WordsRange = xlWS.Cells.Find(Words(i), LookIn:=xlValues, LookAt:=xlWhole)
If Not WordsRange Is Nothing Then
WordsColumn = ConvertToLetter(WordsRange.Cells.Column)
Exit For
End If
Next i
(note: "ConvertToLetter" is another function, converting the number of the column into the corresponding letter - column 1 = A and so on. This function works fine)
I tried like this, basically wrapping the whole block around a Function:
Function SearchWords(Words As Variant) As String
'define search terms
Dim WordsRange As Excel.Range
Dim WordsColumn As Integer
Dim i As Integer
' Look for each input
For i = LBound(Words) To UBound(Words)
Set WordsRange = xlWS.Cells.Find(Words(i), LookIn:=xlValues, LookAt:=xlWhole)
If Not WordsRange Is Nothing Then
WordsColumn = ConvertToLetter(WordsRange.Cells.Column)
Exit For
End If
Next i
End Function
But this gives an error (424) on the line
Set WordsRange = xlWS.Cells.Find(Words(s), LookIn:=xlValues, LookAt:=xlWhole)
Any idea how to solve this?
Thanks a lot
Bookmarks