請問以下程式碼,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
SVAL = Format(Now, "HH") - 1
excel 會把這個當算術題,數學沒有 08 這回事
解決方法
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