Compare two cells, and synthesize the difference.

cheshire

New Member
Joined
Feb 18, 2016
Messages
5
Hi,

I am comparing some data on types of differences: additions and deletions. I've used some code from a previous thread to compare two strings and find difference; and simply switched the WORDDIF arguments accordingly.

=WORDDIF(A1,B1)
Code:
Function WORDDIF(rngA As Range, rngB As Range) As String

Dim WordsA As Variant, WordsB As Variant
Dim ndxA As Long, ndxB As Long, strTemp As String

WordsA = Split(rngA.Text, " ")
WordsB = Split(rngB.Text, " ")

For ndxB = LBound(WordsB) To UBound(WordsB)
For ndxA = LBound(WordsA) To UBound(WordsA)
If StrComp(WordsA(ndxA), WordsB(ndxB), vbTextCompare) = 0 Then
WordsA(ndxA) = vbNullString
Exit For
End If
Next ndxA
Next ndxB

For ndxA = LBound(WordsA) To UBound(WordsA)
If WordsA(ndxA) <> vbNullString Then strTemp = strTemp & WordsA(ndxA) & " "
Next ndxA

WORDDIF = Trim(strTemp)

End Function

However, I have come to some obstacles.


1. Sometimes both the addition and deletion cell will print the same name.
2. I want to delete the numbers with parentheticals "(1)" and "AC"

Updated code

Code:
Function WORDDIF(rngA As Range, rngB As Range) As String

'Remove linefeed characters and exclude parentheticals

Dim WordsA As Variant, WordsB As Variant
Dim ndxA As Long, ndxB As Long, strTemp As String

WordsA = Split(Replace(rngA.Text, vbLf, " "), " ") 'remove linefeed
WordsB = Split(Replace(rngB.Text, vbLf, " "), " ") 'remove linefeed

For ndxB = LBound(WordsB) To UBound(WordsB)
For ndxA = LBound(WordsA) To UBound(WordsA)
If StrComp(WordsA(ndxA), WordsB(ndxB), vbTextCompare) = 0 Then
WordsA(ndxA) = vbNullString
Exit For
End If
Next ndxA
Next ndxB

For ndxA = LBound(WordsA) To UBound(WordsA)
'Exclude parentheticals
If Not WordsA(ndxA) Like "(*)" Then strTemp = strTemp & WordsA(ndxA) & " "
Next ndxA

WORDDIF = Application.Trim(strTemp)

End Function


The code looks great. I have a few changes I would like to make:

3. Ultimately it would be nice to print a new column with: additions (italicized) and deletions (strike through). Instead of having a deletions or additions column (which is great to visually see), I was wondering if there's a code that synthesizes the information, so that the output is: all the school names, with deletions (strike through), additions (italicized), and repeats (no change = normal).

4. Each school is separated (for the additions/deletions column). I think they just come as a list without any separation technique. So either each has its own line within the cell or separated by a column. Perhaps a comma could replace the double parenthetical.

Examples to go with the above:

3: Synthesized information:

B6=[TABLE="class: cms_table_cms_table, width: 110"]
<tbody>[TR]
[TD="class: cms_table_cms_table_xl64, width: 110"]Med Col of Georgia (1) (AC) Morehouse (1) (AC)[/TD]
[/TR]
</tbody>[/TABLE]

D6=
[TABLE="class: cms_table_cms_table, width: 110"]
<tbody>[TR]
[TD="class: cms_table_cms_table_xl66, width: 110"]Med Col of Georgia (1) (AC) Meharry (1) (AC)[/TD]
[/TR]
</tbody>[/TABLE]

Deletions
G6 =WORDDIF(D6,B6)[TABLE="class: cms_table_cms_table, width: 70"]
<tbody>[TR]
[TD="class: cms_table_cms_table_xl66"]Meharry[/TD]
[/TR]
</tbody>[/TABLE]

Additions
H6 = =WORDDIF(B6,D6)
[TABLE="class: cms_table_cms_table, width: 82"]
<tbody>[TR]
[TD="class: cms_table_cms_table_xl66, width: 82"]Morehouse[/TD]
[/TR]
</tbody>[/TABLE]


I6 = {new code or something}

[TABLE="class: cms_table, width: 110"]
<tbody>[TR]
[TD="class: cms_table_xl64, width: 110"]Med Col of Georgia,Morehouse,Meharry*[/TD]
[/TR]
</tbody>[/TABLE]

*Note: I couldn't figure out how to strike through Meharry, so I underlined it.
Also note that there are commas separating the schools in the final list. Perhaps this could be done with replacing the set of parenthetical by commas.

4. Schools separated

B91 =
[TABLE="class: cms_table, width: 110"]
<tbody>[TR]
[TD="class: cms_table_xl66, width: 110"]Columbia (1) (AC)
Northwestern (1) (AC) Southern Calif (1) (AC)
U Chicago-Pritzker (1) (AC) U Michigan (1) (AC)
Wash U St Louis (1) (AC)[/TD]
[/TR]
</tbody>[/TABLE]

D91 =
[TABLE="class: cms_table, width: 110"]
<tbody>[TR]
[TD="class: cms_table_xl66, width: 110"]Northwestern (1) (AC) Southern Calif (1) (AC)
U Chicago-Pritzker (1) (AC) U Michigan (1) (AC)[/TD]
[/TR]
</tbody>[/TABLE]

H91 ==WORDDIF(B91,D91)
[TABLE="class: cms_table, width: 82"]
<tbody>[TR]
[TD="class: cms_table_xl66, width: 82"]Columbia Wash U St Louis[/TD]
[/TR]
</tbody>[/TABLE]

{improved version}
H91 ==WORDDIF(B91,D91)
[TABLE="class: cms_table, width: 82"]
<tbody>[TR]
[TD="class: cms_table_xl66, width: 82"]Columbia
Wash U St Louis[/TD]
[/TR]
</tbody>[/TABLE]

OR
[TABLE="class: cms_table, width: 82"]
<tbody>[TR]
[TD="class: cms_table_xl66, width: 82"]Columbia, Wash U St Louis[/TD]
[/TR]
</tbody>[/TABLE]

Note the spaces in the first example and the comma in the second.

Cheers for all your assistance.

Also, I've done a bit of coding back in the day. While I am a bit rusty, it would be nice to learn how to code for Excel. If you have any pointers or resources, that would be helpful.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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