Rakeshexcel
New Member
- Joined
- Nov 22, 2021
- Messages
- 11
- Office Version
- 365
- Platform
- 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:
TABLE B:
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 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:
Category | Date | Output & desired column |
Sample 1 | 03-Jan-21 | 100 |
Sample 1 | 04-Jan-21 | 120 |
Sample 1 | 24-Jan-21 | 123 |
Sample 1 | 30-Jan-21 | 0 |
Sample 4 | 15-Jan-21 | 732 |
Sample 4 | 15-Jan-21 | 750 |
Sample 4 | 02-Mar-21 | 810 |
Sample 4 | 04-Mar-21 | 0 |
Sample 4 | 07-Mar-21 | 0 |
TABLE B:
Category | Date | Ref. No |
Sample 1 | 24-Jan-21 | 100 |
Sample 1 | 27-Jan-21 | 120 |
Sample 1 | 28-Jan-21 | 123 |
Sample 1 | 01-Feb-21 | 144 |
Sample 4 | 21-Jan-21 | 732 |
Sample 4 | 21-Jan-21 | 750 |
Sample 4 | 10-Mar-21 | 810 |
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