这个脚本:
Option Explicit
Dim goFS : Set goFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = "..\data\updsheet\save.xls"
Dim oExcel : Set oExcel = CreateObject("Excel.Application")
Dim oWBook : Set oWBook = oExcel.Workbooks.Open(goFS.GetAbsolutePathName(sFSpec))
Dim dtA : dtA = #1/3/2013 8:45:43 AM#
Dim dtB : dtB = #9/6/2012 8:04:58 AM#
Dim dtC : dtC = #11/15/2012 8:45:43 AM#
Dim dtD : dtD = #12/7/2012 8:45:43 AM#
Dim aTests : aTests = Array( _
dtA, dtB _
, dtC, dtD _
)
Dim i
For i = 0 To UBound(aTests) Step 2
WScript.Echo "----", i, aTests(i + 0), "<>", aTests(i + 1), aTests(i + 0) - aTests(i + 1), aTests(i + 1) - aTests(i + 0)
On Error Resume Next
WScript.Echo "A", oExcel.Application.WorksheetFunction.Text(aTests(i + 0) - aTests(i + 1), "[h]:mm:ss")
If Err.Number Then WScript.Echo "A", Err.Description
On Error GoTo 0
On Error Resume Next
WScript.Echo "B", oExcel.Application.WorksheetFunction.Text(aTests(i + 1) - aTests(i + 0), "[h]:mm:ss")
If Err.Number Then WScript.Echo "B", Err.Description
On Error GoTo 0
WScript.Echo
Next
oExcel.Quit
及其输出(德语语言环境!):
---- 0 03.01.2013 08:45:43 <> 06.09.2012 08:04:58 119,028298611112 -119,028298611112
A 2856:40:45
B Die Text-Eigenschaft des WorksheetFunction-Objektes kann nicht zugeordnet werden.
---- 2 15.11.2012 08:45:43 <> 07.12.2012 08:45:43 -22 22
A Die Text-Eigenschaft des WorksheetFunction-Objektes kann nicht zugeordnet werden.
B 528:00:00
证明:
- @VBSlover 对问题的描述不可信。#11/15/2012 8:45:43 AM# 和 #12/7/2012 8:45:43 AM# 之间大约有 22 天/528 小时。每个人都在猜测 87:45:55 的来源。(所以这个问题应该被否决)
- 除了@KekuSemau 所述,只要您提供正确的参数, WorksheetFunction.Text() 就会返回合理的字符串值。(因此,如果尝试回答的主题不是主题且有用,则应否决)
- @VBSlovers 代码包含一个 OERN。因此,如果对 TimeSpan(函数名)的赋值有问题,则该函数的返回值是未定义的。难怪有些单元格充满了虚假数据,当您隐藏错误时,这就是您所得到的。
更新一:wrt @Sean 的提议:
如果你跑
Option Explicit
Dim dtA : dtA = #1/3/2013 8:45:43 AM#
Dim dtB : dtB = #9/6/2012 8:04:58 AM#
Dim Sean : Sean = "2856:09:45"
Dim Ekke : Ekke = "2856:40:45"
WScript.Echo "!German Locale!"
WScript.Echo "dtB", dtB
WScript.Echo "dtA", dtA
WScript.Echo "Sean", Sean, "=>", addTS(dtB, Sean)
WScript.Echo "Ekke", Ekke, "=>", addTS(dtB, Ekke)
Function addTS(dtX, sHMS)
Dim aParts : aParts = Split(sHMS, ":")
addTS = dtX
addTS = DateAdd("h", CLng(aParts(0)), addTS)
addTS = DateAdd("n", CLng(aParts(1)), addTS)
addTS = DateAdd("s", CLng(aParts(2)), addTS)
End Function
输出:
!German Locale!
dtB 06.09.2012 08:04:58
dtA 03.01.2013 08:45:43
Sean 2856:09:45 => 03.01.2013 08:14:43
Ekke 2856:40:45 => 03.01.2013 08:45:43
你会意识到肖恩的代码中一定有一个错误。