[英]VBA Excel Convert Date Time to Text
我想將日期時間轉換為文本。
示例:10/22/2019 2:10 PM 應轉換為 43760.59028。 如果我試圖轉換電子表格中的單元格,我知道如何執行此操作,但我正在嘗試轉換日期時間變量。
我試過: vdate = Format(CDate("10/22/2019 2:10 PM"), "@")
我最終得到一個字符串,而不是文本轉換為數字。
如何轉換日期或日期時間並將值存儲在變量中?
Format
產生一個String
,你不希望這樣。
vdate
聲明為Double
:
Dim vdate As Double
現在這將起作用:
vdate = CDate("10/22/2019 2:10 PM")
...但這是從Date
到Double
的隱式類型轉換; 你可以明確表示:
vdate = CDbl(CDate("10/22/2019 2:10 PM"))
...正如斯科特在評論中提到的
這個例子可能對你有幫助
Sub convert_date2txt()
Dim theRange as Range
Set theRange = Selection
Dim sDate As String
For Each theCell In theRange
sDate=Format(theCell,"dd/mm/yyyy")
theCell.Value = sDate
theCell.NumberFormat="@"
Next
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.