2016-07-30 245 views
0

我正在学习VBA基础知识。我在变量中有一天,一个月,一年,一小时,一分钟,二次数据。我如何从这些值构造日期时间对象?如何在VBA中为宏编程定义自定义日期时间对象

+0

你尝试过什么引用类型的各个部分?你看过'DateSerial'和'TimeSerial'函数吗?搜索Excel如何存储日期/时间的帮助。 –

回答

1

通常,日期只会表示为Date数据类型。日期存储为双精度数字,表示自1899年12月31日以来的天数。时间表示为一天中的一小部分,例如,上午6:00以.25表示。

然后,您可以指定使用这种方法值日期变量

Dim myDate As Date 
myDate = Now() 
myDate = DateValue("30 July 2016") 
myDate = DateSerial(2016,7,30) 
' or even myDate = DateSerial(2016,7,30+20) which is equivalent to myDate = DateSerial(2016,8,19) 
myDate = TimeValue("19:22:56") 

,或者如果你有一个包含你的年,月,日,等变量,你可以计算日期值

myDate = DateSerial(myYear, myMonth, myDay) + TimeSerial(myHour, myMinute, mySecond) 

如果你不想使用Date数据类型,你可以使用类似

Type myDateType 
    myDay as Integer 
    myMonth as Integer 
    myYear as Integer 
    myHour as Integer 
    myMinute as Integer 
    mySecond as Integer 
End Type 

,然后通过类似

Dim myDateVariable as myDateType 

声明变量,并通过类似

myDateVariable.myYear = 2016 
相关问题