Fredrik1987
Board Regular
- Joined
- Nov 5, 2015
- Messages
- 69
Hi!
I've been trying to solve this problem for a couple of days now without any luck.
I wnat to search a range of cells based on their values. But somehow the instr() function wont work, I've used it earlier with sucsess, so I don't understand why it wont work now.
So far I'm just testing the code, so the words I want to look for are inserted into "A1" and search through "A2:A6". As it is now, using instr() inserts "OK" into all the cells in column B.
This is how the sheet looks like:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Column A[/TD]
[TD]Column B[/TD]
[TD]Column C[/TD]
[/TR]
[TR]
[TD]YES, NO[/TD]
[TD][/TD]
[TD]YES[/TD]
[/TR]
[TR]
[TD]YES[/TD]
[TD][/TD]
[TD]NO[/TD]
[/TR]
[TR]
[TD]yes[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]no[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]maybe[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]well yes[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
I've tried different functions without any luck, I've used the instr() function the EXACT same way earlier and it works perfectly.
I've been trying to solve this problem for a couple of days now without any luck.
I wnat to search a range of cells based on their values. But somehow the instr() function wont work, I've used it earlier with sucsess, so I don't understand why it wont work now.

So far I'm just testing the code, so the words I want to look for are inserted into "A1" and search through "A2:A6". As it is now, using instr() inserts "OK" into all the cells in column B.
This is how the sheet looks like:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Column A[/TD]
[TD]Column B[/TD]
[TD]Column C[/TD]
[/TR]
[TR]
[TD]YES, NO[/TD]
[TD][/TD]
[TD]YES[/TD]
[/TR]
[TR]
[TD]YES[/TD]
[TD][/TD]
[TD]NO[/TD]
[/TR]
[TR]
[TD]yes[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]no[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]maybe[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]well yes[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Code:
Sub Test()
' VIRKER
Dim rSearch_Target As Range
Dim cell As Range
Dim S
Dim N
Dim i As Integer
Dim wordcount As Integer
Dim iLastRow_Søkeord As Integer
Dim sWords_To_Search_For As String
Dim vArray_SearchWords As Variant
Dim sArray_Search_Words
Set rSearch_Target = Sheets("Sheet1").Range("A1")
'' Number of words to search for, used to redim the size of the search array.
For Each cell In rSearch_Target
S = Application.WorksheetFunction.Trim(cell.Text)
N = 0
'Hvis S ikke er tom then
If S <> vbNullString Then
N = Len(S) - Len(Replace(S, " ", "")) + 1
End If
' Antall ord i søkestrengen
wordcount = wordcount + N
Next
'' Search range and search array, split search words with comma (,)
sWords_To_Search_For = Sheets("Sheet1").Range("A1").Value
vArray_SearchWords = Split(sWords_To_Search_For, ", ")
iLastRow_Søkeord = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
ReDim sArray_Search_Words(0 To wordcount)
'' This part is just to verify that the array is correct, so far the script works just fine
For i = 0 To UBound(vArray_SearchWords)
sArray_Search_Words(i) = vArray_SearchWords(i)
Cells(i + 1, "C").Value = sArray_Search_Words(i)
Next i
For Each cell In Range("A2:A" & iLastRow_Søkeord)
For i = 0 To wordcount
' Here's the problem, earlier I've just the exact line, and it works just fine, but somehow it finds that all the cells contain my search strings
' If I ommit Instr() it works just as planned
If sArray_Search_Words(i) = cell.Value _
Or InStr(1, cell.Value, sArray_Search_Words(i)) > 0 Then
cell.Offset(, 1).Value = "OK"
End If
Next i
Next
End Sub
I've tried different functions without any luck, I've used the instr() function the EXACT same way earlier and it works perfectly.