Subtracting from a date in VBA?

calendar_today Asked Aug 8, 2011
thumb_up 18 upvotes
history Updated April 16, 2026

Question posted 2011 · +9 upvotes

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.

Accepted answer +18 upvotes

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")

External references cited (2)

  • techonthenet.com — http://www.techonthenet.com/excel/formulas/dateadd.php
  • office.microsoft.com — http://office.microsoft.com/en-us/access-help/dateadd-function-HA001228810.aspx

Top vba Q&A (6)

+18 upvotes ranks this answer #20 out of 81 vba solutions on this site — top 25%.
vba