Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
KelleyS
 
Posts: n/a
Default Ignore Non-Alphanumerics When Comparing Text

I am comparing the values in two cells using a macro. The data I'm comparing
is coming from two different sources. These sources may have slightly
different values in the corresponding cells, particularly the inclusion or
absence of non-alpha characters.

For instance, one cell might have "ABC, Inc." while another has "ABC, Inc"
(no period after Inc). The period is not material for my purposes, so I'd
like the values to still pass the compare test as equal. Similarly, I'd like
to ignore commas and hyphens for the comparison. Is there a way to do this?
  #2   Report Post  
Harlan Grove
 
Posts: n/a
Default

KelleyS wrote...
I am comparing the values in two cells using a macro. The data I'm comparing
is coming from two different sources. These sources may have slightly
different values in the corresponding cells, particularly the inclusion or
absence of non-alpha characters.

For instance, one cell might have "ABC, Inc." while another has "ABC, Inc"
(no period after Inc). The period is not material for my purposes, so I'd
like the values to still pass the compare test as equal. Similarly, I'd like
to ignore commas and hyphens for the comparison. Is there a way to do this?


If you have Windows Script Host installed (and you do if you have
Internet Explorer 5.0 or higher installed), then you could use its
regular expression class to eliminate hyphens and replace all sequences
of nonalphanumeric characters with spaces. Then compare the resulting
strings. Something like


Sub cmp()
Dim n As Long, s1 As String, s2 As String
Dim re As Object

Set re = CreateObject("VBScript.RegExp")
re.Global = True

For n = 1 To 3
s1 = Range("A1:A3").Cells(n, 1).Value
s2 = Range("B1:B3").Cells(n, 1).Value

re.Pattern = "-"
s1 = re.Replace(s1, "")
s2 = re.Replace(s2, "")

re.Pattern = "[^A-Za-z0-9]+"
s1 = re.Replace(s1, " ")
s2 = re.Replace(s2, " ")

re.Pattern = " +"
s1 = re.Replace(s1, " ")
s2 = re.Replace(s2, " ")

Range("C1:C3").Cells(n, 1).Value = (Trim(s1) = Trim(s2))
Next n

Set re = Nothing
End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing text and returning boolean values nicoleeee Excel Discussion (Misc queries) 9 December 2nd 05 10:43 AM
Formulas dealing with text data Bagia Excel Worksheet Functions 6 June 20th 05 10:29 PM
Comparing text in columns Lear Excel Discussion (Misc queries) 1 June 8th 05 09:35 PM
Sort or Filter option? Mcobra41 Excel Worksheet Functions 3 February 23rd 05 07:22 PM
Ignore text Function Grant Excel Worksheet Functions 3 February 3rd 05 10:38 PM


All times are GMT +1. The time now is 11:24 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"