从VBA的日期减去? [英] Subtracting from a date in VBA?

查看:182
本文介绍了从VBA的日期减去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel VBA中的日期操作时遇到很大的问题。
我有一个表单,其中有一个用户将输入日期的文本框。问题是他可能会以不同的格式输入(例如,8月1日的1.08.2011或同一天的8/1/11)。现在我想做的是减去从他进入TextBox的日期开始的几天。到目前为止我已经取得了成功,我不知道该怎么做。
我尝试过这样的东西

 格式((格式(Me.datalivrare.Value,dd.mm.yyy ) -  4),dd.mm.yyyy)

其中datalivrare是那个文本框,其中用户输入日期,4是要从该日期减去的天数...,而且我希望格式始终为dd.mm.yyyy,无论他们在该文本框中输入什么。

解决方案

我建议查看VBA的DateAdd函数



http://www.techonthenet.com/excel/formulas/dateadd.php



http://office.microsoft.com/en-us/access- help / dateadd-function-HA001228810.aspx



您可以执行以下操作:

 格式(DateAdd(d,-4,CDate(Me.datalivrare.Value)),dd.mm.yyyy)
pre>

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

这篇关于从VBA的日期减去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆