Excel VBA - If row values are found in another column, return specific value in the corresponding row values in another column

TropicalMagic

New Member
Joined
Jun 19, 2021
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Hi all,


I would like to check if row values in Workbooks("B.xlsx").Sheets(2).Range("AN2:AN" & LASTROW) can be found in Workbooks("A.xlsx").Sheets(1).Range("D1:D30"), then corresponding row values in Workbooks("B.xlsx").Sheets(2).Range("AM2:AM" & LASTROW) = "YES"

If row values cannot be found, then do nothing.


Here is my (non-functional) code so far:

VBA Code:
Dim ws As Worksheet, ws2 As Worksheet
Dim j, LASTROW As Long
Dim arrD, arrFin As Variant
Dim answer, found As Range
  
Set ws = Workbooks("A.xlsx").Sheets(1)
Set ws2 = Workbooks("B.xlsx").Sheets(2)
LASTROW = ws2.Range("A" & Rows.Count).End(xlUp).Row
arrD = ws.Range("D1:D30").Value 
ReDim arrFin(1 To UBound(arrD), 1 To 1) 
For j = 1 To UBound(arrD)
    answer = ws.Range("H" & j).Value
    
    Set found = ws2.Columns("AN2:AN" & LASTROW).Find(what:=answer)
    If found Is Nothing Then
        Nothing 
    Else
        arrFin(j, 1) = "YES"  
    End If
Next j
ws2.Range("AM2").Resize(UBound(arrFin), 1).Value = arrFin


Many thanks!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

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