Re: Seaching selected words using VBA
Quote
a word from column D can correspond with multiple terms (The word "niet" can be found in every cell in column B blad 2). So I need the corresponding results to apear in the same row starting with column F
I have modified the macro and given the macro new name "test_revised"
Note: your E6 in blad1 is blank. I have inserted x in that cell to avoid that blank
Sub test_revised()
Dim x As String, cfind As Range, r1 As Range, c1 As Range, summary As Range
Dim add As String
Application.ScreenUpdating = False
With Worksheets("blad1")
Range(.Range("F1"), .Cells(1, Columns.Count)).EntireColumn.Cells.Clear
End With
With Worksheets("Blad1")
Set r1 = Range(.Range("D1"), .Range("D1").End(xlDown))
For Each c1 In r1
x = c1
With Worksheets("Blad2")
Set cfind = .Columns("B:B").Find(what:=x, lookat:=xlPart)
add = cfind.Address
If Not cfind Is Nothing Then
Set summary = cfind.Offset(0, -1)
'msgbox summary
Else
GoTo nextc1
End If
End With
'.Cells(c1.Row, "F") = summary
.Cells(c1.Row, Columns.Count).End(xlToLeft).Offset(0, 1) = summary
'With Worksheets("Blad2")
Do
With Worksheets("Blad2")
Set cfind = .Cells.FindNext(cfind)
If cfind.Address = add Then Exit Do
If cfind Is Nothing Then Exit Do
'If Not cfind Is Nothing Then
Set summary = cfind.Offset(0, -1)
'msgbox summary
'Else
'GoTo nextc1
'End If
End With
.Cells(c1.Row, Columns.Count).End(xlToLeft).Offset(0, 1) = summary
Loop
nextc1:
Next c1
End With
MsgBox "macro done"
Application.ScreenUpdating = True
End Sub
Display More
" it also has to work with selected or idividual cells in column D, and not just the entire column."
instead of looping through D1 to D6 you can remove that FOR -NEXT and use the cell address with a dot e.g. .range("D2"). try modifying the new macro for this and give a new name, save the file and test
after running test_revised Blad1 will be like this
1 lokaal A Spijbelaars
2 donkere C Little Bit Love
3 drugs F Little Bit Love The Drug Lord
4 zin F The Drug Lord
5 wereld A Little Bit Love The Drug Lord
6 niet x Little Bit Love The Drug Lord Spijbelaars