iT邦幫忙

0

vba Format(Now, "HH")問題

  • 分享至 

  • xImage

https://ithelp.ithome.com.tw/upload/images/20220922/20109231nAqqVSaeFm.png

請問以下程式碼,Format(Now, "HH")明明是雙位數小時制,為何產出仍為H單位數小時,又該如何修正為"08"呢?非常感謝~~~

Sub TEST()
SVAL = Format(Now, "HH") - 1
SVAL = Format(Date, "YYYY/M/D") & " " & SVAL & ":00"
With ActiveWorkbook.SlicerCaches("Slicer_更新時間")
.ClearManualFilter
For Each oSlicerItem In .SlicerItems
If oSlicerItem.Name = SVAL Then
oSlicerItem.Selected = True
Else
oSlicerItem.Selected = False
End If
Next oSlicerItem
End With
End Sub

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

3
blanksoul12
iT邦研究生 5 級 ‧ 2022-09-22 10:25:37
最佳解答

SVAL = Format(Now, "HH") - 1
excel 會把這個當算術題,數學沒有 08 這回事
解決方法

  1. SVAL = Format(TimeValue(Now) - TimeValue("1:00:00"), "HH")
  2. SVAL = format(Format(Now, "HH") - 1,"00")
看更多先前的回應...收起先前的回應...

blanksoul12 大感謝又解決我一個頭痛的難題

blanksoul12 iT邦研究生 5 級 ‧ 2022-09-22 11:56:57 檢舉

根據 rogeryao 的
SVAL = Format(DateAdd("h", -2, Now), "HH") 也不錯

blanksoul12 iT邦研究生 5 級 ‧ 2022-09-22 12:00:18 檢舉

不是IT部而且IT也不幫忙的話,學好 vba 可以幫妳省很多很多時間,可能不回公司也可以正常運行.

rogeryao iT邦超人 8 級 ‧ 2022-09-22 12:12:33 檢舉

可能要測一下當 Now = "2022-9-2 0:6:2"

blanksoul12 iT邦研究生 5 級 ‧ 2022-09-22 12:18:57 檢舉

看要求和一般運時間,如果是手動運行,應該可以不用確認日子問題,要看他自己了

1
rogeryao
iT邦超人 8 級 ‧ 2022-09-22 10:28:26
Private Sub CommandButton1_Click()
Dim xNow As Date
Dim xHour As String

xNow = "2022-9-2 0:6:2"
xHour = Hour(DateAdd("h", -1, xNow))
MsgBox (String(2 - Len(xHour), "0") & xHour)
'
Cells(1, 1).NumberFormat = "@"
Cells(1, 1) = String(2 - Len(xHour), "0") & xHour

End Sub

rogeryao 非常感謝幫忙喔~~

我要發表回答

立即登入回答