Excel Vba Compare 2 strings but only return full words found

diamanthian1

New Member
Joined
Jun 23, 2014
Messages
8
I have 2 worksheets with lists of strings in each
worksheet"validation"
Wolf Team
Bite Back
Walk Alone

<tbody>
</tbody>

Worksheets"data"
AARDWOLF
ABALONE
ABACK

<tbody>
</tbody>




I running some code to test if any word in "Validation" occurs in "Data" and for each occurrence the individual word found should be placed in the next column in the "Data" sheet and the whole String should be placed in column after that.
I have written the following code which I thought would do it

Code:
[FONT=arial]Sub searchStringTest()[/FONT]
[FONT=arial]  Dim i As Long, j As Long[/FONT]
[FONT=arial]  Dim ws1 As Worksheet, ws2 As Worksheet[/FONT]
[FONT=arial]  Dim WrdArray() As String[/FONT]
[FONT=arial]  Dim text_string As String[/FONT]

[FONT=arial]  Set ws1 = Sheets("Validation")[/FONT]
[FONT=arial]  Set ws2 = Sheets("Data")[/FONT]

[FONT=arial]  For i = 1 To ws2.Range("A" & ws2.Rows.Count).End(xlUp).Row[/FONT]
[FONT=arial]    For j = 1 To ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row[/FONT]
[FONT=arial]        text_string = UCase(ws1.Range("A" & j))[/FONT]
[FONT=arial]        WrdArray() = Split(text_string)[/FONT]
[FONT=arial]            For K = LBound(WrdArray) To UBound(WrdArray)[/FONT]
[FONT=arial]        If InStr(1, UCase(ws2.Range("A" & i)), WrdArray(K)) Then[/FONT]
[FONT=arial]         ws2.Range("B" & i) = WrdArray(K)[/FONT]
[FONT=arial]         ws2.Range("C" & i) = text_string[/FONT]
[FONT=arial]        Else[/FONT]


[FONT=arial]        End If[/FONT]
[FONT=arial]        Next K[/FONT]


[FONT=arial]    Next j[/FONT]
[FONT=arial]  Next i[/FONT]

[FONT=arial]  Set ws1 = Nothing: Set ws2 = Nothing[/FONT]
[FONT=arial]End Sub[/FONT]

but instead of any occurrences of the full words I am getting
Worksheets"data"
AARDWOLFWolfWolf Team
ABALONEAloneWalk Alone
ABACKBackBite Back

<tbody>
</tbody>

So I am getting Stings that are contained within the words, instead of just the words.
Any Ideas I am still new to coding and am pretty sure I have just missed something simple
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
What is the result for the sheet "data" you want for the sample data you have posted?
 
Upvote 0
For the examples given it should be
AARDWOLF
ABALONE
ABACK

<tbody>
</tbody>

as none of them should match
That seems to be the reverse of your original request: -->test if any word in "Validation" occurs in "Data".
Do you mean: test if any word in "Data" occurs in "Validation"?
 
Upvote 0
Thanks for replying Joe,
The code is correct, it's just not doing what I need
none of them should match as AARDWOLF does not equal WOLF, ABALONE does not equal ALONE
WOLF is within AARDWOLF, but that is not what I am trying to achieve
if you see what I mean
 
Upvote 0
Thanks for replying Joe,
The code is correct, it's just not doing what I need
none of them should match as AARDWOLF does not equal WOLF, ABALONE does not equal ALONE
WOLF is within AARDWOLF, but that is not what I am trying to achieve
if you see what I mean
I think you need to reverse the order of comparison in your code. Your code should ask if any word in sheet "Data" is found in the sheet "Validation" and it seems to be doing the reverse.
 
Upvote 0

Forum statistics

Threads
1,215,361
Messages
6,124,500
Members
449,166
Latest member
hokjock

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top