当我尝试在单元格 mismatch Error
"J8"
如何修改添加小时?
With Range("J8")
.Value = "='" & FilePath & "[Report.xlsm]Data'!J8" '// J8 = 02/12/2016 08:17 AM CST
.Value = Mid(.Value, 12, 12) '// J8 = 08:17 AM CST
.Value = .Value + TimeSerial(1, 0, 0) ' <-- Error mismatch
End With
答案 0 :(得分:2)
您可能必须自己从字符串转换时间。 Mid function返回一个字符串;转换为TimeValue。 TimeValue不接受时区缩写,因此必须将其删除。
dim sTZ as string
With Range("J8")
.Value = "='" & FilePath & "[Report.xlsm]Data'!J8" '// J8 = 02/12/2016 08:17 AM CST
sTZ = Mid(.Value, 21, 3)
.Value = TimeValue(Mid(.Value, 12, 8)) '// J8 = 08:17 AM (No time zone)
.Value = .Value + TimeSerial(1, 0, 0) ' <-- Time is added to time; no mismatch
'optionally put the time zone back in
'.value = .value & chr(32) & sTZ
'optionally keep the time as time and format the time zone back in
'.numberformat = "hh:mm AM/PM C\ST"
End With
如果保持与UTC时间的关系是关键任务,那么您将不得不连接后端的时区信息。这将转换表示时区的字符串中的时间值。如果您只是更改了Range.NumberFormat property。
,则可以避免转换为字符串将大量时区硬编码为自定义数字格式可能不切实际。以下内容构建了一个自定义数字格式,其中包含原始文本的相应时区,看起来像是一次性。
Dim sTZ As String, sTZmask As String
With Range("J8")
.Value = "='" & FilePath & "[Report.xlsm]Data'!J8" '// J8 = 02/12/2016 08:17 AM CST
sTZ = Mid(.Value, 21, 3)
'split the time zone and stitch it back together with the number format escape character
sTZmask = Join(Split(StrConv(Chr(32) & sTZ, vbUnicode), vbNullChar), Chr(92))
'remove any trailing escape characters
Do While Right(sTZmask, 1) = Chr(92): sTZmask = Left(sTZmask, Len(sTZmask) - 1): Loop
.Value = TimeValue(Mid(.Value, 12, 8)) '// J8 = 08:17 AM (No time zone)
.Value = .Value + TimeSerial(1, 0, 0) ' <-- Time is added to time; no mismatch
'keep the time as time and format the time zone back in
.NumberFormat = "hh:mm AM/PM" & sTZmask
End With
答案 1 :(得分:0)
With Range("J8")
.Value = "='" & FilePath & "[Report.xlsm]Data'!J8" '// J8 = 02/12/2016 08:17 AM CST
.Value = TimeValue(Mid(.Value, 12, 8)) '// J8 = 08:17 AM CST
.NumberFormat = "hh:mm AM/PM C\ST"
.Value = .Value + TimeSerial(1, 0, 0) '// Add 1 Hour 09:17
End With
谢谢Jeeped