Results 1 to 7 of 7

Thread: Comparing 2 cells....

  1. #1

    Thread Starter
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Comparing 2 cells....

    Alright, I know how I would do this in VBA. But, I was curious to see if anyone knew of a formula I could use to compare the values of 2 cells to see how similar they are.

    I was unable to find any such function that could be used in formulas, so I will just go ahead and work on my VBA function.

    But, I'll check back and see if anyone has anything for this.

    Thanks.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Comparing 2 cells....

    Wouldnt it be just an IF comparison?

    VB Code:
    1. =IF(A2=A3,"Match","NoMatch")
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: Comparing 2 cells....

    Well, I can do that... But what I need to know is if there are any similarities.... More like if they have any common words.

    I can do it easily in VBA, but, just... I am LAZY right now.

    So say you take:

    Muffins are very good

    And

    Muffins are not very good at all.

    Compare them and, well, figure out how much they have in common. But, as I said... I seriously doubt there is a way to do this with formulas......

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Comparing 2 cells....

    Oh more like a fuzzy match comparison. Hmm, nope nothing built in for that.

    Probably just split the cell into an array split on the space char?
    Then loop comparing the target cell with an Instr function comparison.
    Or maybe a .Find method passing the array element.

    I'm feeling lazy too. Was out at a clients site today. Time for a nap
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: Comparing 2 cells....

    Haha, good reply, that's exactly what I am doing and it is working great thus far. Now time for some statistical crap and we're ready!

    Thanks a lot.

  6. #6
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: Comparing 2 cells....

    It's funny you guys mention this, I just gave someone some code exactly how RobDog described....

    Say you have your main values on sheet "Blad1" in column B and your new (or comparison) values on sheet "Blad2" in Column B.....

    I know you said you got it working, but just to compare:
    VB Code:
    1. Option Explicit
    2. Sub CompareStrings()
    3.     If TypeName(Selection) <> "Range" Then Exit Sub
    4.     Dim wsBlad1 As Excel.Worksheet, wsBlad2 As Excel.Worksheet
    5.     Dim cell As Excel.Range, cellMain As Excel.Range
    6.     Dim rngBlad1 As Excel.Range, rngBlad2 As Excel.Range
    7.     Dim strMain As String, strCompare As String
    8.     Dim SplitMain As Variant, SplitCompare As Variant
    9.     Dim i As Long, j As Long, ItemMatch As Long, count As Long
    10.     Dim Percents() As Single, StringMain() As String
    11.     Dim StringCompare() As String
    12.     Dim sPercent As Single
    13.     ReDim Percents(0)
    14.     ReDim StringMain(0)
    15.     ReDim StringCompare(0)
    16.     Set wsBlad1 = Worksheets("Blad1")
    17.     Set wsBlad2 = Worksheets("Blad2")
    18.     Set rngBlad1 = wsBlad1.Range("B2:B" & wsBlad1.Cells(Rows.count, "B").End(xlUp).Row)
    19.     Set rngBlad2 = wsBlad2.Range("B2:B" & wsBlad2.Cells(Rows.count, "B").End(xlUp).Row)
    20.     For Each cell In rngBlad2
    21.         SplitCompare = Split(WorksheetFunction.Trim(cell.Text), " ")
    22.          
    23.         For Each cellMain In rngBlad1
    24.             SplitMain = Split(WorksheetFunction.Trim(cellMain.Text), " ")
    25.             ItemMatch = 0
    26.              
    27.             For i = LBound(SplitCompare) To UBound(SplitCompare)
    28.                 For j = LBound(SplitMain) To UBound(SplitMain)
    29.                     If UCase(SplitCompare(i)) = UCase(SplitMain(j)) Then
    30.                         ItemMatch = ItemMatch + 1
    31.                         Exit For
    32.                     End If
    33.                 Next
    34.             Next
    35.              
    36.             sPercent = Round(((ItemMatch) / (UBound(SplitMain) + 1)) * 100, 2)
    37.              
    38.              'Exit Sub
    39.             If sPercent > 0 Then
    40.                  
    41.                 ReDim Preserve Percents(0 To count)
    42.                 ReDim Preserve StringMain(0 To count)
    43.                 ReDim Preserve StringCompare(0 To count)
    44.                  
    45.                 Percents(count) = sPercent
    46.                 StringMain(count) = cellMain.Text
    47.                 StringCompare(count) = cell.Text
    48.                 count = count + 1
    49.             End If
    50.              
    51.             If sPercent = 100 Then
    52.                 Exit For 'May not want this...but unsure
    53.             End If
    54.         Next cellMain
    55.     Next cell
    56.     With Sheets("Blad3")
    57.         .Cells.Clear
    58.          
    59.         .Range("A1") = "Percent"
    60.         .Range("B1") = "Main Database Text"
    61.         .Range("C1") = "New data text"
    62.          
    63.         .Range("A2:A" & count + 1).Value = WorksheetFunction.Transpose(Percents)
    64.         .Range("B2:B" & count + 1).Value = WorksheetFunction.Transpose(StringMain)
    65.         .Range("C2:C" & count + 1).Value = WorksheetFunction.Transpose(StringCompare)
    66.     End With
    67. End Sub




    If you find any of my posts of good help, please rate it

  7. #7

    Thread Starter
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: Comparing 2 cells....

    That is essentially what I did... A little less code however. It's simple and works nicely.

    Thanks for all the replies.

Posting Permissions

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



Click Here to Expand Forum to Full Width