INSTR or MATCH - Unsure how to tackle logic

Sphinx404

Board Regular
Joined
May 2, 2015
Messages
186
Office Version
  1. 365
Platform
  1. Windows
Good Morning Experts,

Please see code below. I tried to explain what I want to do, but I'm having trouble putting it all together. Any solutions, guidance or advice would be appreciated.

Code:
Sub HELPME()


Dim lrow As Long
Dim myworksheet As Worksheet
Dim myWorksheet2 As Worksheet
Dim lrow2 As Long


Set myworksheet = Worksheets("Sheet1")
lrow = myworksheet.Cells(myworksheet.Rows.Count, "A").End(xlUp).row
Set myWorksheet2 = Worksheets("Sheet2")
lrow2 = myWorksheet2.Cells(myWorksheet2.Rows.Count, "A").End(xlUp).row


Application.ScreenUpdating = False
Application.DisplayAlerts = False


r = 2


Do Until r = 2 & lrow2


lookup_cell = Sheet1.Range("B" & r)[COLOR=#008000] 'look up this cell - SHEET1[/COLOR]
lookup_range = Sheet2.Range("B" & r) [COLOR=#008000]'look for it in this range - SHEET2[/COLOR]
compare_cell1 = Sheet2.Range("A" & r) [COLOR=#008000]'IF FOUND,compare_cell1 vs. compare_cell2[/COLOR]
compare_cell2 = Sheet1.Range("A" & r)
return_value = Sheet1.Range("A" & r) [COLOR=#008000]'If the compared cells match, then return TRUE, otherwise FALSE in SHEET1 "C2"[/COLOR]


[COLOR=#ff0000]        if istr(lookup_cell,[/COLOR]
    
r = r + 1


Loop


End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
That last line should read

Code:
return_value = [COLOR=#ff0000][B]Sheet2[/B][/COLOR].Range("A" & r) [COLOR=#008000]'If the compared cells match, then return TRUE, otherwise false in SHEET1 "C2"[/COLOR]
 
Upvote 0
Is this what you're after
Code:
Sub HELPME()


Dim lrow As Long
Dim myworksheet As Worksheet
Dim myWorksheet2 As Worksheet
Dim lrow2 As Long


Set myworksheet = Worksheets("Sheet1")
lrow = myworksheet.Cells(myworksheet.Rows.Count, "A").End(xlUp).Row
Set myWorksheet2 = Worksheets("Sheet2")
lrow2 = myWorksheet2.Cells(myWorksheet2.Rows.Count, "A").End(xlUp).Row


Application.ScreenUpdating = False
Application.DisplayAlerts = False


R = 2


Do Until R = lrow2
   Sheet1.Range("C2").Value = False
   If Sheet1.Range("B" & R).Value = Sheet2.Range("B" & R).Value Then
      If Sheet2.Range("A" & R).Value = Sheet1.Range("A" & R).Value Then
         Sheet1.Range("C2").Value = True
         Exit Do
      End If
   End If
       
   R = R + 1
Loop



End Sub
Not sure if the sheet names are correct, as you seem to be mixing sheet names & sheet codenames
 
Upvote 0
I'm sorry if it confuses you... I just wanted to make it as generic as possible so it was more easily understood. I've been working this problem all **** day... Search, Index, Match, Find, etc. etc... Articles, videos... I'm at my wits end with this problem.

It's directly related to this post I put up a couple of months back, but couldn't work it out:

https://www.mrexcel.com/forum/excel-questions/1031225-vlookup-table-user-defined-argument.html

Still the same problem, and it's not the logic I'm having trouble with... it's the coding.
 
Upvote 0
Not quite sure If I've understood correctly, but maybe something like this
Code:
Do Until R = lrow2
   Sheet1.Range("C2").Value = False
   If InStr(1, Sheet1.Range("B" & R).Value, Sheet2.Range("B" & R).Value, vbTextCompare) > 0 Then
      If Sheet2.Range("A" & R).Value = Sheet1.Range("A" & R).Value Then
         Sheet1.Range("C2").Value = True
         Exit Do
      End If
   End If
       
   R = R + 1
Loop
If the value from sheet1 exists anywhere in the value from sheet2 it will move to the next if
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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