返回列表 上一主題 發帖

[發問] 寫一個天數累加的自訂函數

回復 1# naraohya


不知道有沒有理解錯誤
直接讓D欄記住第一次B欄選PD時的日期(這用CHANGE事件來輸入C,D欄的值)
Sheet3模組
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. If Target.Count > 1 Then Exit Sub
  3. If Intersect(Target, [B3:B100]) Is Nothing Or Target = "" Then Exit Sub
  4. Application.EnableEvents = False
  5. If Target.Offset(, 1) = "" Then Target.Offset(, 1).Resize(, 2) = Array(IIf(Target = "PD", 1, ""), IIf(Target = "PD", Date, ""))
  6. Application.EnableEvents = True
  7. End Sub
複製代碼
每次開檔時計算D欄日期到當日的天數並更改D欄為當日日期
  1. Private Sub Workbook_Open()
  2. Dim A As Range
  3. With Sheet3
  4. Application.EnableEvents = False
  5. For Each A In .[B3:B100]
  6.    If A = "PD" And IsDate(A.Offset(, 2)) Then _
  7.    A.Offset(, 1) = _
  8.    DateDiff("d", A.Offset(, 2), Date) + A.Offset(, 1): A.Value = "CL": A.Offset(, 2) = Date
  9. Next
  10. Application.EnableEvents = True
  11. End With
  12. End Sub
複製代碼
計算pending day.zip (11.84 KB)
學海無涯_不恥下問

TOP

        靜思自在 : 甘願做、歡喜受。
返回列表 上一主題