I'm having big problems doing operation with the date in Excel VBA. I have a form that has a textbox where the user will enter the date. The problem is that he may enter it in different formats (eg, 1.08.2011 for 1st of August, or 8/1/11 for the same day). Now what I want to do is to subtract some days from that date that he enters in the TextBox. I had to success so far and I don't know how to do it. I tried something like this
Format((Format(Me.datalivrare.Value, "dd.mm.yyy") - 4), "dd.mm.yyyy")
Where datalivrare is that textbox where the user enters the date and 4 is the number of days I want to subtract from that date... and I want the format to always be dd.mm.yyyy no matter what they enter in that textbox.
I suggest looking at the DateAdd function for VBA
http://www.techonthenet.com/excel/formulas/dateadd.php
http://office.microsoft.com/en-us/access-help/dateadd-function-HA001228810.aspx
You could do the following:
Format(DateAdd("d", -4, CDate(Me.datalivrare.Value)), "dd.mm.yyyy")
First cast to Date, then subtract days, then format appropriately:
Format(DateAdd("d", -4, CDate(Me.datalivrare.Value)), "dd.mm.yyyy")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With