Consulting

Results 1 to 3 of 3

Thread: Compare column values

  1. #1

    Compare column values

    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.
    234.png
    Attached Images Attached Images
    Last edited by bossofalan; 12-14-2021 at 05:16 AM.

  2. #2
    VBAX Newbie
    Joined
    Dec 2021
    Posts
    3
    Location
    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

  3. #3
    Quote Originally Posted by DaDoc View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •