用VBA编程显示当前日期

用VBA编程显示当前日期,第1张

Sub 当前日期()

Dim idate As Date

idate = Date

ActiveSheetRange("A1")Value = idate

End Sub

系统自带的now 或者Timer函数都只能获取到秒,一个相关的API,使用Windows API的winmmdll其中的timeGetTime 可以获得毫秒数,这个值是从开机到现在的毫秒数。

Private Declare Function timeGetTime Lib "winmmdll" () As Long

Private Sub btnTest_Click()

Dim StartMS As Long

Dim EndMS As Long

Dim MS As Long

StartMS = timeGetTime() '开始毫秒数

While timeGetTime < StartMS + 200 '循环等待

DoEvents '转让控制权,以便让 *** 作系统处理其它的事件

Wend

EndMS = timeGetTime() '结束毫秒

MS = EndMS - StartMS '取两者相差的毫秒数

MsgBox "毫秒数:" & MS

End Sub

While timeGetTime < StartMS + 200 '循环等待

DoEvents '转让控制权,以便让 *** 作系统处理其它的事件

Wend

上面三句代码网友可以更换成自己实际计算过程的代码。

所需工具材料:EXCEL(以2007版本为例)

方法如下:

一、Now 函数

Now函数返回当前系统日期和时间

添加一个按钮,并添加以下功能:

Private Sub Constant_demo_Click()

Dim a as Variant

a = Now()

msgbox("The Value of a : " & a)

End Sub

二、Hour函数

Hour函数返回一个介于0和23表示当天指定时间戳的小时。

添加一个按钮,并添加以下功能:

Private Sub Constant_demo_Click()

msgbox("Line 1: " & Hour("3:13:45 PM"))

msgbox("Line 2: " & Hour("23:13:45"))

msgbox("Line 3: " & Hour("2:20 PM"))

End Sub

三、Minute函数

Minute函数返回一个介于0和59表示小时内指定时间戳记的分钟。

添加一个按钮,并添加以下功能:

Private Sub Constant_demo_Click()

msgbox("Line 1: " & Minute("3:13:45 PM"))

msgbox("Line 2: " & Minute("23:43:45"))

msgbox("Line 3: " & Minute("2:20 PM"))

End Sub

四、Second函数

Second函数返回一个介于0和59,代表小时指定时间戳记的秒值。

添加一个按钮,并添加以下功能:

Private Sub Constant_demo_Click()

msgbox("Line 1: " & Second("3:13:25 PM"))

msgbox("Line 2: " & Second("23:13:45"))

msgbox("Line 3: " & Second("2:20 PM"))

End Sub

五、Time函数

Time函数返回当前系统时间。

Private Sub Constant_demo_Click()

msgbox("Line 1: " & Time())

End Sub

六、Timer函数

Timer函数(定时器)返回自12:00 AM秒和毫秒数。

添加一个按钮,并添加以下功能:

Private Sub Constant_demo_Click()

msgbox("Time is : " & Now())

msgbox("Timer is: " & Timer())

End Sub

以上就是关于用VBA编程显示当前日期全部的内容,包括:用VBA编程显示当前日期、VBA中 怎么获得时间的毫秒数 用time只能得到秒,毫秒怎么算出来啊、excel vba 怎么使用时间函数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9625534.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存