麻辣家族討論版版's Archiver

dou10801 發表於 2023-5-4 16:14

EXCEL 對1900年以前的日期,如何相加減

[i=s] 本帖最後由 dou10801 於 2023-5-4 16:21 編輯 [/i]

請教各位先進,EXCEL 對1900年以前的日期,如何相加減.
[例]:  1850/5/20-1850/5/15=5

Andy2483 發表於 2023-5-4 16:18

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121161&ptid=23981]1#[/url] [i]dou10801[/i] [/b]


    [url]http://forum.twbts.com/thread-23971-1-2.html[/url]

Andy2483 發表於 2023-5-5 07:59

[i=s] 本帖最後由 Andy2483 於 2023-5-5 08:05 編輯 [/i]

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121161&ptid=23981]1#[/url] [i]dou10801[/i] [/b]


    謝謝前輩發表此主題
後學學習方案如下,請前輩參考

執行結果:
[attach]36302[/attach]

Sub TEST_20230505_1()
MsgBox CDate("1850/5/20") - CDate("1850/5/15")
End Sub

Sub TEST_20230505_2()
Dim D1 As Date, D2 As Date
D1 = "1850/5/20": D2 = "1850/5/15"
MsgBox D1 - D2
End Sub

Sub TEST_20230505_3()
MsgBox CDate("1850/5/15") + 5
End Sub

Sub TEST_20230505_4()
MsgBox CDate("1850/5/20") - 5
End Sub

dou10801 發表於 2023-5-5 16:11

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121173&ptid=23981]3#[/url] [i]Andy2483[/i] [/b]如附件,敬請前輩指導,謝謝.

Andy2483 發表於 2023-5-8 11:04

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121177&ptid=23981]4#[/url] [i]dou10801[/i] [/b]


    謝謝前輩回復
後學藉此帖練習陣列與公曆日期的檢測,學習方案如下,請前輩參考

執行前:
[attach]36310[/attach]

檢測出非公曆日:
[attach]36311[/attach]

修正日期後執行結果:
[attach]36312[/attach]


Option Explicit
Sub TEST()
Dim Brr, Crr, i&, N&, Ds As Date, Dn As Date
Brr = Range([A2], Cells(Rows.Count, "A").End(3))
ReDim Crr(1 To UBound(Brr), 1 To 2)
For i = 1 To UBound(Brr)
   If Not IsDate(Brr(i, 1)) Then Crr(i, 1) = "←非公曆日": N = N + 1
Next
[color=SeaGreen]'↑先設迴圈檢測日期是否為正確公曆日期[/color]
If N > 0 Then [B2].Resize(UBound(Crr), 2) = Crr: MsgBox "資料錯誤! 請修正後再執行!": Exit Sub
[color=SeaGreen]'↑如果有錯必須先修正錯誤,再重新執行[/color]
For i = 1 To UBound(Brr) - 1
   Crr(i, 1) = Brr(i + 1, 1) & "-" & Brr(i, 1)
   Ds = Brr(i + 1, 1): Dn = Brr(i, 1): Crr(i, 2) = Ds - Dn
[color=SeaGreen]   '↑由於變數宣告,會自動將字串轉換為日期[/color]
Next
[B2].Resize(UBound(Crr), 2) = Crr
Erase Brr, Crr
End Sub

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供