format

来源:百度文库 编辑:神马文学网 时间:2024/04/29 06:05:46
format 

【名称】
Format
【类别】
格式输出函数
【原形】
Format(expression[, format[, firstdayofweek[, firstweekofyear]]])
或Format$(表达式[,”格式化符号”])
【参数】
表达式是要格式化的数值、日期和字符串类型表达式。格式化符号是表示输出表达式值时所采用的输出格式。格式化符号要用引号括起来。
Format 函数的语法具有下面几个部分:
部分
说明
expression
必要参数。任何有效的表达式。
format
可选参数。有效的命名表达式或用户自定义格式表达式。
firstdayofweek
可选参数。常数,表示一星期的第一天。
firstweekofyear
可选参数。常数,表示一年的第一周。

例子:

Private Sub CommandButton2_Click()

   t1 = Format([a1], "yyyymmddhhmmss")
   t2 = Format([a2], "yyyymmddhhmmss")
   MsgBox Format(t2 - t1, "###0年#0月#0日#0时#0分#0秒")

End Sub

其他保存:

Private Sub CommandButton1_Click()
H1 = Cells(1, 1)
y1 = Year(H1) & Application.Rept("0", 2 - Len(Month(H1))) & Month(H1) & Application.Rept("0", 2 - Len(Day(H1))) _
& Day(H1) & Application.Rept("0", 2 - Len(Hour(H1))) & Hour(H1) & Application.Rept("0", 2 - Len(Minute(H1))) & Minute(H1) _
& Application.Rept("0", 2 - Len(Second(H1))) & Second(H1)
H2 = Cells(2, 1)
y2 = Year(H2) & Application.Rept("0", 2 - Len(Month(H2))) & Month(H2) & Application.Rept("0", 2 - Len(Day(H2))) _
& Day(H2) & Application.Rept("0", 2 - Len(Hour(H2))) & Hour(H2) & Application.Rept("0", 2 - Len(Minute(H2))) & Minute(H2) _
& Application.Rept("0", 2 - Len(Second(H2))) & Second(H2)
If y2 > y1 Then
MsgBox "A1日期" & y1 & Chr(10) _
& "A2日期" & y2 & Chr(10) _
& "A1时间早,2个时间差" & Application.Rept("0", 14 - Len(y2 - y1)) & y2 - y1
Else
MsgBox "A1日期" & y1 & Chr(10) _
& "A2日期" & y2 & Chr(10) _
& "A2时间早,2个时间差" & Application.Rept("0", 14 - Len(y1 - y2)) & y1 - y2
End If
End Sub