VBA First in First out by dates

Rakeshexcel

New Member
Joined
Nov 22, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to extract a reference number from a database using FIFO method. Have tried numerous ways, while i could get to write some code with google ref., i couldn't really achieve the full result. Please help me.

I am sharing the details along with codes i had worked. To assist, i have pre-populated expected values in "Output & desired column" in which the vba has to throw same results. Please feel free to erase the details in this column

Step 1. I want the code to lookup dates from each category in Table B and see if there are any dates lesser to the one its searching in same category from Table A
Step 2. If found, the Ref. No from Table B should updated in Table A, corresponding to column of lesser date found
Step 3. If no lesser dates are found in Table A for a particular category, VBA to pick the next date & do the similar step 1 & 2
Step 4. Once the above step is completed for a particular category, VBA to pick next category.

Imp Note: If a lesser date found in Table A, the ref. no from Table B needs to be populated in output column from Table A. Once the Ref. no is used, VBA should pick the next date only.

TABLE A:

CategoryDateOutput & desired column
Sample 103-Jan-21100
Sample 104-Jan-21120
Sample 124-Jan-21123
Sample 130-Jan-210
Sample 415-Jan-21732
Sample 415-Jan-21750
Sample 402-Mar-21810
Sample 404-Mar-210
Sample 407-Mar-210

TABLE B:

CategoryDateRef. No
Sample 124-Jan-21100
Sample 127-Jan-21120
Sample 128-Jan-21123
Sample 101-Feb-21144
Sample 421-Jan-21732
Sample 421-Jan-21750
Sample 410-Mar-21810

Code:
Sub test()
Dim srow As Range
Dim sdate, sdate1 As Date
Dim chk As String
Dim sl_no As String
Set Rng = Selection
'Loop through sheet B data
Range("B1").Sort key1:=Sheets(2).Range("B1"), Order1:=xlAscending, Header:=xlYes
For Each cel In ThisWorkbook.Sheets("B").Range("A2:C" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Rows
sdate = Sheets(2).Range("B" & cel.Row).Value
sl_no = Sheets(2).Range("C" & cel.Row).Value
MsgBox sdate


flag1:
For Each cel1 In ThisWorkbook.Sheets("A").Range("A2:C" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Rows
chk = Sheets(1).Range("C" & cel1.Row).Value
If chk = "" Then
sdate1 = Sheets(1).Range("B" & cel1.Row).Value
If sdate1 < sdate Then
Sheets(1).Range("C" & cel1.Row).Value = sl_no
End If
MsgBox "Sheet A" & sdate1
End If
Next cel1
Next cel
End Sub
 
I did not understand your question. but yes, the logic has to be based on both category and Date. At present, the formula works based on date only
So you mean to say you need to apply logic based on category and date . Hmmm

I suggest to keep Table B as a based that keeps updating each time in table A . but for that Table A needs to be start from Scratch . is that possible ?
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hello, am not sure if you have stopped working on this & i know you would have drained your energy on this. But i really need this, thanks for all your support
 
Upvote 0
Hello, am not sure if you have stopped working on this & i know you would have drained your energy on this. But i really need this, thanks for all your support
I don't see another scenario where your second logic will apply.

If you can share another small sample with both logic applicable maybe then I can assist.

also give answer table as to how you want the data to appear
 
Upvote 0
HI,

thanks for responding. have attached a different sample also provided the logic to be in table B that can be referred. Column D in table A is the expected outcome

Table ATable B
ABCDFGHI
CategoryRef. NoDateOutput & desired columnCategoryRef. NoDateLogic
Sample 1110003-Jan-21100Sample 110024-Jan-21Ref. no alloted to D3, lesser than date found in sample 1 of table A
Sample 1121204-Jan-21120Sample 112027-Jan-21Ref. no alloted to D4, next lesser than date found in sample 1 of table A
Sample 1130415-Feb-21156Sample 112328-Jan-21Ref. no NOT ALLOTED, no next lesser date found in sample 1 of table A
Sample 1132220-Feb-21163Sample 114401-Feb-21Ref. no NOT ALLOTED, no next lesser date found in sample 1 of table A
Sample 4194815-Jan-21732Sample 115619-Feb-21Ref. no alloted to D5, next lesser than date found in sample 1 of table A
Sample 4224915-Jan-21750Sample 116301-Mar-21Ref. no alloted to D6, next lesser than date found in sample 1 of table A
Sample 4230402-Mar-21810Sample 473221-Jan-21Ref. no alloted to D7, lesser than date found in sample 4 of table A
Sample 4432504-Mar-210Sample 475021-Jan-21Ref. no alloted to D8, lesser than date found in sample 4 of table A
Sample 4304207-Mar-210Sample 481010-Mar-21Ref. no alloted to D9, lesser than date found in sample 4 of table A
Sample 7322203-Jan-21900Sample 790028-Jan-21Ref. no alloted to D12, lesser than date found in sample 7 of table A
Sample 7394804-Jan-21901Sample 790128-Jan-21Ref. no alloted to D13, lesser than date found in sample 7 of table A
Sample 7200208-Mar-210Sample 792114-Jan-21Ref. no NOT ALLOTED, no next lesser date found in sample 7 of table A
Sample 721220-Mar-210
Sample 749222-Mar-210
Sample 7203523-Mar-210
 
Upvote 0
Table B:
- Is the last Ref of Sample 7 14-Feb-21? I am assuming date is increasing per specific sample.
- Is the Ref unique thru out all categoried? I found no duplicate Ref in table B
 
Upvote 0
Yes thats right, dates are sorted from oldest to newest and Ref No. is unique in table B.

But where did you find 14-Feb-21 in sample 7? I assume you meant 14-Jan-21 as last ref in sample 7, if yes then 14-Jan-21 is the last ref in sample 7. Similarly, sample 4 last ref. is 10-mar-21 and sample 1 last ref. is 1-mar-21
 
Upvote 0
HI,

thanks for responding. have attached a different sample also provided the logic to be in table B that can be referred. Column D in table A is the expected outcome

Table ATable B
ABCDFGHI
CategoryRef. NoDateOutput & desired columnCategoryRef. NoDateLogic
Sample 1110003-Jan-21100Sample 110024-Jan-21Ref. no alloted to D3, lesser than date found in sample 1 of table A
Sample 1121204-Jan-21120Sample 112027-Jan-21Ref. no alloted to D4, next lesser than date found in sample 1 of table A
Sample 1130415-Feb-21156Sample 112328-Jan-21Ref. no NOT ALLOTED, no next lesser date found in sample 1 of table A
Sample 1132220-Feb-21163Sample 114401-Feb-21Ref. no NOT ALLOTED, no next lesser date found in sample 1 of table A
Sample 4194815-Jan-21732Sample 115619-Feb-21Ref. no alloted to D5, next lesser than date found in sample 1 of table A
Sample 4224915-Jan-21750Sample 116301-Mar-21Ref. no alloted to D6, next lesser than date found in sample 1 of table A
Sample 4230402-Mar-21810Sample 473221-Jan-21Ref. no alloted to D7, lesser than date found in sample 4 of table A
Sample 4432504-Mar-210Sample 475021-Jan-21Ref. no alloted to D8, lesser than date found in sample 4 of table A
Sample 4304207-Mar-210Sample 481010-Mar-21Ref. no alloted to D9, lesser than date found in sample 4 of table A
Sample 7322203-Jan-21900Sample 790028-Jan-21Ref. no alloted to D12, lesser than date found in sample 7 of table A
Sample 7394804-Jan-21901Sample 790128-Jan-21Ref. no alloted to D13, lesser than date found in sample 7 of table A
Sample 7200208-Mar-210Sample 792114-Jan-21Ref. no NOT ALLOTED, no next lesser date found in sample 7 of table A
Sample 721220-Mar-210
Sample 749222-Mar-210
Sample 7203523-Mar-210
Is this what you want

Book1
ABCDEFGHI
1Table ATable B
2ABCDFGHI
3CategoryRef. NoDateOutput & desired columnCategoryRef. NoDateLogic
4Sample 1110003-Jan-21100Sample 110024-Jan-21Ref. no alloted to D3, lesser than date found in sample 1 of table A
5Sample 1121204-Jan-21120Sample 112027-Jan-21Ref. no alloted to D4, next lesser than date found in sample 1 of table A
6Sample 1130415-Feb-21750Sample 112328-Jan-21Ref. no NOT ALLOTED, no next lesser date found in sample 1 of table A
7Sample 1132220-Feb-210Sample 114401-Feb-21Ref. no NOT ALLOTED, no next lesser date found in sample 1 of table A
8Sample 4194815-Jan-21144Sample 115619-Feb-21Ref. no alloted to D5, next lesser than date found in sample 1 of table A
9Sample 4224915-Jan-21156Sample 116301-Mar-21Ref. no alloted to D6, next lesser than date found in sample 1 of table A
10Sample 4230402-Mar-210Sample 473221-Jan-21Ref. no alloted to D7, lesser than date found in sample 4 of table A
11Sample 4432504-Mar-210Sample 475021-Jan-21Ref. no alloted to D8, lesser than date found in sample 4 of table A
12Sample 4304207-Mar-210Sample 481010-Mar-21Ref. no alloted to D9, lesser than date found in sample 4 of table A
13Sample 7322203-Jan-21900Sample 790028-Jan-21Ref. no alloted to D12, lesser than date found in sample 7 of table A
14Sample 7394804-Jan-21901Sample 790128-Jan-21Ref. no alloted to D13, lesser than date found in sample 7 of table A
15Sample 7200208-Mar-210Sample 792114-Jan-21Ref. no NOT ALLOTED, no next lesser date found in sample 7 of table A
16Sample 721220-Mar-210
17Sample 749222-Mar-210
18Sample 7203523-Mar-210
Sheet1
Cell Formulas
RangeFormula
D4:D18D4=IFERROR(INDEX($G$4:$G$15,AGGREGATE(15,6,ROW($H$4:$H$15)-ROW($H$4)+1/(C4<=$H$4:$H$15)*($F$4:$F$15=$A4),ROWS(C$4:C4))),0)
 
Upvote 0
Not exactly. Cell D6 (Sample 1) has picked up the ref. no from sample 4, while it has to search lesser date within sample 1 only.
 
Upvote 0
Corrected statement:
Not exactly. Cell D6 (Sample 1) has picked up the ref. no from sample 4, while it has to search next higher date within sample 1 only.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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