Dlookup With Dates Not Working

MCTampa

Board Regular
Joined
Apr 14, 2016
Messages
97
I have a text box called Current that populates todays date on load.
I want the next text two text boxes (Prior and Y2) to populate the same day prior year and the same day two year's ago by looking up those dates off of a table.
I don't want to do date()-365 and date()-728 because I want the user to be able to select a different date in the Current box.

On button click I want Prior and Y2 to update.

I'm using the following sub (trying to solve for Prior for the moment).

VBA Code:
Private Sub Command7_Click()
Prior = DLookup("[Year1]", "Dates", "#" & Format(Forms!Form1![Current], "mm\/dd\/yyyy\") & "#")
End Sub

My "Dates" table runs from 1/1/2021 through 12/31/2021 and has two columns Year1 and Year2 which run from 1/3/2020 and 1/4/2019.

Today, the current box populates with 5/25/2021 and when I press my button, I only get 1/3/2020 for my Prior box.

Not sure what I'm doing wrong with the formatting, but evidently something is amiss.

Thanks,
Mike
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Instead of a DLookup() this might be easier...

VBA Code:
Prior = DateSerial(Year(Forms!Form1![Current]) - 1, Month(Forms!Form1![Current]), Day(Forms!Form1![Current]))
 
Upvote 0
Or DateAdd function?
Me.Prior = DateAdd("yyyy", -1, Me.Current) ? Not sure I see the need for a table of dates unless you want to avoid dates that are weekends or holidays. That is a different matter.

Current is not a name I would use for anything:
 
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