Comparing Ranges - Partial Match VBA or Formula Request

Teddy.B.Baker

New Member
Joined
May 25, 2011
Messages
4
I need some help on adapting a VBA module. The original module is useful for me when I am looking for exact matches, but now I need to find partial matches. Is this possible? For example in the CompareRange there might be a value of 93472398742398-118 but somewhere within the selection the value is only 93472398742398. The CompareRange is several thousand rows and the selection is much smaller.
I found the original formula here: How to compare data in two columns to find duplicates in Excel.

Code:
<code>Sub Find_Matches()
    Dim CompareRange As Variant, x As Variant, y As Variant
    ' Set CompareRange equal to the range to which you will
    ' compare the selection.

    Set CompareRange = Range("F1:F5126")
    ' NOTE: If the compare range is located on another workbook
    ' or worksheet, use the following syntax.
    ' Set CompareRange = Workbooks("Book2"). _ 
    ' Worksheets("Sheet2").Range("F1:F5126")'
    
    ' Loop through each cell in the selection and compare it to
    ' each cell in CompareRange.
    For Each x In Selection 
        For Each y In CompareRange 
            If x = y Then x.Offset(0, 1) = x 
        Next y
    Next x
End Sub
</code>I also tried to adapt a formula that I found in one of the Stack Overflow answers:
Determine if name in list A has perfect match, partial match, or is not found in list B?
but Excel says there's an error in the formula. However, I don't understand what the error is.

<code>=MATCH(FALSE;ISERROR(SEARCH(B1;A1:A100));0)</code>I'm somewhat at a loss now. Thanks for any help. Please note that I am still a novice when it comes to Excel formulas and VBA.
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Something like this might work (untested)...

Code:
  For Each x In Selection 
    For Each y In CompareRange 
      If x = y Then x.Offset(0, 1) = x 
    Next y
  Next x

  For Each x In Selection 
    For Each y In CompareRange 
      if instr(1,x,y)>0 then 
        ans = msgbox("Does " & x & " contain "y "?",vbyesno)
        
        if ans = vbyes then x.Offset(0, 1) = x 
      end if
    Next y
  Next x

Displaying the msgbox will allow you to test the new code. If it seems to work correctly, then remove the "ans = "... and "if ans = " and leave the "x = x.offset"...

Hope this helps.


Tim
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

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