Find in range with multiple criteria and if no match, look in another range -- VBA

ouvay

Board Regular
Joined
Jun 9, 2022
Messages
131
Office Version
  1. 2019
Platform
  1. Windows
Hello,

[CASE 1]
I am working on a code to loop through my data, find through the worksheet based on 2 criterias and offset to the column on the right of the find result.


[CASE 2] -- If [CASE 1] is not a match
BUT if find isn't a match of both criterias... then look through second range.

This is what I have so far and its pretty far off from what I need.

VBA Code:
Set Rng = Rws1.Range("F2:Z" & Rws1.Cells(Rows.Count, 3).End(xlUp).Row) 'Rws is [CASE1] Range
    Set Rng2 = Rws2.Range("F2:Z" & Rws2.Cells(Rows.Count, 3).End(xlUp).Row) 'Rws is [CASE2] Range
    For i = 2 To lastrow ' lastrow is last row of data
    If ws.Cells(i, 22).Value = "TRUE" Then 'Boolean to activate find -- ignore for all intent and purposes
    Else
    Set Rng = Rws1.Range("F2:Z" & Rws1.Cells(Rows.Count, 3).End(xlUp).Row).Find(what:=ws.Range("F" & i), lookat:=xlWhole, LookIn:=xlValues)
    Do While Not Rng Is Nothing
    ws.Range("W" & i).Value = Rng.Offset(0, 1).Value  ' at this point, code finds first match and gives me the value on the right of the found column -- Ignoring the criteria because I haven't worked that in...
        Set Rng = rang.FindNext(after:=Rng)
        Loop
        End If
    Next i




Here is a sample of the data - In the highlighted row you will see expected result in column W


Book1
ABCDEFGHIJKLMNOPQRSTUVW
1Payment input CSV output targetSheet NameItem Line NoComplementSlip NumberBuyer IdSeller IdPaymentPayment Type CodePayment DatePayment AmountScheduled PaymentScheduled Payment of Tax ExcludedScheduled Other PaymentApply Reason CodePayment RemarksSeller TOKUREI StatusBuyer TOKUREI StatusCanceledAmount paidPrefix SignExactTarget
224562123112470024700130024700MTRUE
3625623411123501235065012350MTRUE
462345623411123501235065012350MTRUE
523456512511123501235065012350MTRUE
623456125411123501235065012350MTRUE
7136136511440052252755225MFALSE4987.5
82356161711123501235065013000MTRUE
93467711123501235065012350MTRUE
1034612313104501045055010450MTRUE
Sheet1



Here is example of data range to find in .. there is a second sheet similar to this which would be the second range to look through - highlighted in this sheet is the match that I need to offset and bring back to ws.range("W" & i).value =


Book2
ABCDEFGHIJKLMN
1NoteRecovery ReasonSale IDDistributor IDTotal Recovery AmountRecovery ID 1Recovered Amount 1Recovery ID 2Recovered Amount 2Recovery ID 3Recovered Amount 3Recovery ID 4Recovered Amount 4
21231673861361363420136133318
312324987.513613614987.5
412331235013612350
512344987.5136134987.5
612354987.51234987.51361312351361364987.5
7345643701361361364370
834571995013619950
9345852251361361365225
1034599975361369975
1134512320212.53613612512.5367700
12345168692.513614987.51363705
134561233420363420
144562346840366840
154563454987.5361235136123513613123513611282.5
16456151220901362090
1745642090362090
Sheet1



I apologize if this isn't enough information and will be happy to provide more
TIA

EDIT := If there are multiple matches (in either case) the amounts would need to be added up
 
Last edited by a moderator:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,187,202
Messages
5,962,181
Members
438,592
Latest member
vincethesun

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
Top