Return nearest date after finding match ID

cloudy123456

New Member
Joined
Jul 26, 2018
Messages
4
I have 2 sets of data. One contains ID# and dates . The other set contains same ID # and different dates. Each ID # occurs multiple times, but not equal number of times, in each spreadsheet. I want a formula to look for ID # match in the second spreadsheet and return the date that is nearest to the date in spreadsheet formula is in.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Lots of unanswered questions here. Im not going to ask them all because its bed time. I will only ask one.
Main date is 5/15/2019
Lookup is 5/17/2019
2nd lookup is 5/13/2019
How is this handled.
 
Upvote 0
Main sheet has inventory ID# and date of shipping. Ancillary sheet has inventory ID# and date rcvd. Should be within a week of each other. How can I get this in one sheet?
 
Upvote 0
If that’s the only way to solve it. I’m fairly skilled with excel formulas but not so much with vba. Willing to try it tho.
 
Upvote 0
Wait dont use this one. Its not done. I forgot to incorporate the code for the ID must be matching. In that case I cant use a For Each loop and this wont work at all. Sorry but i gotta go to sleep and the loop you need is more complicated.

Lets use a custom function. Works just like a formula but you gotta code it in VBA.
Code:
Function myFunction(lookupDate, lookupRange)
   [COLOR=#008000]  'lookupDate = Date of shipping
     'lookupRange = Dates received [/COLOR]
     myVariable = 999999999
     For Each element in lookupRange
          If element < myVariable _
          And element > lookupDate Then
               myVariable = element
          End If
     Next element
     myFunction = myVariable
End Function
=myFunction(B2, Ancillary!$B$2:$B$9999)
 
Last edited:
Upvote 0
Can't sleep when a problem is not solved.

Lets use a custom function. Works just like a formula but you gotta code it in VBA.
Code:
Function myFunction(lookupID, lookupDate, lookupRange)
  [COLOR=#008000]   'lookupID = ID of shipping[/COLOR]
    [COLOR=#008000] 'lookupDate = Date of shipping
     'lookupRange = IDs and Dates received, Columns A and B
[/COLOR]     Application.Volatile
     myVariable = 999999999
     myBool = True
     For Each element in lookupRange
          If myBool = True Then
               If lookupID = element Then
                    myBool = False
               End If
          Else
                If element < myVariable _
                And element > lookupDate Then
                     myVariable = element
                End If
                myBool = True
          End If
     Next element
     myFunction = myVariable
End Function
=myFunction(A2, B2, Ancillary!$A$2:$B$9999)
 
Last edited:
Upvote 0
Im getting error ...im sure its user error on my part with VBA. Excuse my ignorance on VBA, but do i have to change the code at all? I made sure my column names are the same as stated in the code.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

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