PDA

View Full Version : [SOLVED:] Compare column values



bossofalan
12-14-2021, 05:06 AM
I'm new to the VBA so I need help.
I want to compare the values from two columns, but there is a line break separete the contents. So what I think is that using arr1, arr2 read the cell values and split it end with the line break. Then, store it into arr3 and arr4 so that I can compare. The program would find the differnce and highlight it. Of course here I use = sign to compare because it is easiler to do so. So that, I want to first convert the font in column B to red and highlight the equal words to black. Below is my works please help! Thank you.

Sub compare()
Dim arr1, arr2, arr3, arr4
Dim rng1 As Range, rng2 As Range
'Code for select two column or range??
arr3 = Split(arr1, Chr(10))
arr4 = Split(arr2, Chr(10))
'Code for delete words after / i.e. make "Apple / JP" to "Apple" I guess
For i = LBound(arr3) To UBound(arr3)
For j = LBound(arr4) To Ubound(arr4)
If arr4(j) = arr3(i) Then arr2(j).Font.vbRed
Next j
Next i
End Sub

This is the effect I want.
29227

DaDoc
12-14-2021, 05:43 PM
Sub Compare_column_values()
Dim arr$()
Dim sReferenceString$
Dim i%, f%
arr = Split(Cells(2, 1), vbLf)
sReferenceString = Cells(2, 2)
For i = LBound(arr) To UBound(arr)
f = InStr(sReferenceString, arr(i))
Do While f > 0
Cells(2, 2).Characters(Start:=f, Length:=Len(arr(i))).Font.Color = vbRed
f = InStr(f + 1, sReferenceString, arr(i))
Loop
Next
End Sub
maybe this will help you

bossofalan
12-15-2021, 03:26 AM
Sub Compare_column_values()
Dim arr$()
Dim sReferenceString$
Dim i%, f%
arr = Split(Cells(2, 1), vbLf)
sReferenceString = Cells(2, 2)
For i = LBound(arr) To UBound(arr)
f = InStr(sReferenceString, arr(i))
Do While f > 0
Cells(2, 2).Characters(Start:=f, Length:=Len(arr(i))).Font.Color = vbRed
f = InStr(f + 1, sReferenceString, arr(i))
Loop
Next
End Sub
maybe this will help you

It works. Thank you. I was just confused how to highlight the similarities.