Comparing characters in two strings and looking for a difference

dgparryuk

New Member
Joined
Nov 21, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

i've just spent 2 hours searching and trying lots of solutions to this from what i found and nothing is quite right for what I want. I'm trying to play with words and find word patterns, i'm trying to find anagrams of words with an extra letter

eg

mod - mood
mod - doom
mod - mode
dos - mods
ood - doom

so my nice simple code was
VBA Code:
For y = 1 To Len(firstword)
                    For x = 1 To Len(secondword)
                        firstletter = Mid(firstword, y, 1)
                        secondletter = Mid(secondword, x, 1)
                            If firstletter = secondletter Then
                                secondwordcount = secondwordcount + 1
                                Exit For ' this fixed some double letter issues but not all
                            Else
                                secondwordcount = secondwordcount
                                
                            End If
                    Next x
                Next y
If secondwordcount = 3 Then

and then check how many letters match

so mod-mode would count 3
dos to mods would also count 3
but mod to mood would count 6 - m-1 o-2 o-2 d-1

i tried removing double letters from the shorter word and changing the calculation based on that
od(ood) to mood would be 3 great
od(ood) to doda would also come out as 3 - d-2 o-1

and not all words are 3 & 4 letters, the longest words are 10 letters long
ABANDONER & ABANDONERS which 9 letters are the same but double A & double N would count 13

as part of other bits i've been doing i do have a list of all the words sorted in alphabetical character order so MOD would be DMO and MOOD would be DMOO, not sure if that would be helpful for this in anyway but thought i'd throw it in incase someone came up with the idea
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Is this what you are after?
Code:
Sub test()
    Dim e
    For Each e In Array(Array("MOD", "MOOD"), Array("ABANDONER", "ABANDONERS"), _
                                  Array("mod", "doom"))
        MsgBox e(0) & " : " & e(1) & vbLf & vbLf & CountMatch(e(0), e(1)), , e(0) & " : " & e(1)
    Next
End Sub

Function CountMatch(ByVal s1$, ByVal s2$) As Long
    Dim i&, temp$
    For i = 1 To Len(s1)
        temp = Replace(s2, Mid$(s1, i, 1), "", , 1, 0)
        If s2 <> temp Then CountMatch = CountMatch + 1
        s2 = temp
    Next
End Function
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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