PDA

View Full Version : Compare two strings



jam03
09-09-2008, 07:57 AM
Hi guys this is actually my first post in here :)

I'm having a little problem and I thought you guys might come in help. Thank you in advance.

I am building my first excel Macro for one of my work.
Indeed, I have to compare about 2000 words (titles) stored in one column, and highlight the ones that are either the same or slightly different.

I wrote the code and I've managed to highlight the recurrent titles but here comes my problem. I also, like I said, need to to highlight the "slightly" different titles. By slightly, I mean that there can be either one letter or two different. For example : [Ecuador] and [Ecuadora] should be compared as positive.

Therefore, I need your help to code the comparison... Maybe I should play with the ASCII numbers, the number of characters in one title or I don't know... Is it actually possible using VBE?

I hope i've been clear, thank you very much for your time,

cheers,

Mateo

jam03
09-09-2008, 08:02 AM
Here, i thought you guys might want to take a look at my code in order to make it clearer of what I've done so far and what's left...


Sub Hlight_exact_name_extended()
'Mateo | Premier | Ecuador

Dim varCounter As Double
Dim varCounter2 As Double

Sheets("sheet1").Select
Range("C1").Select

For varCounter = 0 To 30
For varCounter2 = varCounter + 1 To 30

If Selection.Offset(varCounter, 0).Value = Selection.Offset(varCounter2, 0).Value Then

With Selection.Offset(varCounter, 0).Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
With Selection.Offset(varCounter2, 0).Interior
.ColorIndex = 6
.Pattern = xlSolid
End With

End If

Next
Next


End Sub

Kenneth Hobs
09-09-2008, 08:34 AM
Try LIKE.

jam03
09-10-2008, 03:29 AM
Thank you Kenneth,

I've been trying to use the Like operator, but the only examples found on the internet was about comparing a string to a specific string, and what i'm trying to do is basically this :

For varCounter = 0 To 30

For varCounter2 = varCounter + 1 To 30

StringA = Selection.Offset(varCounter, 0).Value
StringB = Selection.Offset(varCounter2, 0).Value

bool = StringA Like StringB 'Here, i'd like to get bool true if StringA is lets say "Victoria" and StringB is "Victoria1"

If bool = True Then

With Selection.Offset(varCounter, 0).Interior
.ColorIndex = 8
.Pattern = xlSolid
End With
With Selection.Offset(varCounter2, 0).Interior
.ColorIndex = 8
.Pattern = xlSolid
End With

End If

Next

Next



Thanks for you're help

Kenneth Hobs
09-10-2008, 05:15 AM
Use wildcard characters for the comparison string for LIKE. Press F1 at the word LIKE in the VBE for help.
Sub t()
MsgBox "StringA" Like "String*", , "StringA Like String*"
MsgBox "StringA" Like "String", , "StringA Like String"
MsgBox "StringA" Like "String?", , "StringA Like String?"
MsgBox "StringA" Like "String??", , "StringA Like String??"
MsgBox "StringAAA" Like "String??", , "StringAAA Like String??"
End Sub

jam03
09-10-2008, 08:40 AM
Ho I got it ! :)

Thank you very much for your Help Kenneth.

Best regards,

Mateo