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
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You need to update missing date data from Table2 to table 1 right ?
 
Upvote 0
The third column in table A is the outcome am looking for.

For ex:

1. The first sample 1 record in Table B is 24-01-2021 having a ref. No of 100. If there is any date lesser to 24-01-2021 found in table A, then the Ref. No 100 should populate in Table A. In this ex, its 3-01-2021 in Table A, hence ref no. Is 100 corresponding to the date
2. Now ref no. 100 is already used, so the macro has to pick next date in Table B, find if there are any lesser date to that in Table A and move the ref. No accordingly in the corresponding least date found

Hope this helps
Can you give 2 examples and highlight the answer in yellow for understanding

O
 
Upvote 0
Try this

Book1
ABCDEFG
1Table ATable B
2CategoryDateOutput & desired columnCategoryDateRef. No
3Sample 103-Jan-21100Sample 124-Jan-21100
4Sample 104-Jan-21120Sample 127-Jan-21120
5Sample 124-Jan-21123Sample 128-Jan-21123
6Sample 130-Jan-210Sample 101-Feb-21144
7Sample 415-Jan-21732Sample 421-Jan-21732
8Sample 415-Jan-21750Sample 421-Jan-21750
9Sample 402-Mar-210Sample 410-Mar-21810
10Sample 404-Mar-210
11Sample 407-Mar-210
Sheet1
Cell Formulas
RangeFormula
C3:C11C3=IFERROR(INDEX($G$3:$G$9,SMALL(IF(B3<=$F$3:$F$9,ROW($F$3:$F$9)-ROW($F$3)+1),ROWS(B$3:B3))),0)
Press CTRL+SHIFT+ENTER to enter array formulas.


Value 850 will not populate on 2nd March of Table A because lesser date is not found in table B
 
Upvote 0
Fantastic... Just a small tweak required. The date calculation in the above formula is isolated. Date checks & ref. no extraction should happen based on the categories (Column A).
 
Upvote 0
Fantastic... Just a small tweak required. The date calculation in the above formula is isolated. Date checks & ref. no extraction should happen based on the categories (Column A).d

Fantastic... Just a small tweak required. The date calculation in the above formula is isolated. Date checks & ref. no extraction should happen based on the categories (Column A).
What is the difference i didn't get it . I only focused on the answer that you required. :sleep:
 
Upvote 0
Am sorry if i din't put it across righly. i will have numerous categories in both Table A & Table B. My search should be relevant to a particular category, as i will have to run this formula for more than 1000 rows with atleast 100 different categories.
 
Upvote 0
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

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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