返回列表 上一主題 發帖

[發問] Excel VBA 條件成立後,存檔時提醒

[發問] Excel VBA 條件成立後,存檔時提醒

Dear 各位先進,
Excel VBA 是否可以依據"今天日期"且有"帳面錯誤",一旦成立後,存檔時會提醒呢?
再麻煩大家,感恩!!


001.jpg
2020-11-9 19:03



存檔時會提出警告畫面.rar (8.92 KB)
Just do it.

  1. Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  2. Dim d As Date, a, b, r&, c&, t As Boolean
  3. r = [c1].CurrentRegion.Rows.Count - 1
  4. c = Columns("aj").Column
  5. a = Cells(2, 1).Resize(r, c).Value
  6. b = Cells(2, "aq").Resize(r).Value
  7. For i = 1 To UBound(a)
  8.     d = a(i, 3)
  9.     If a(i, c - 1) > a(i, c) Then b(i, 1) = "帳面錯誤": If d = Date Then t = True
  10. Next
  11. Cells(2, "aq").Resize(r) = b
  12. If t Then MsgBox "請注意" & Date & "帳面錯誤"
  13. End Sub
複製代碼

TOP

回復 2# ikboy

Dear ikboy,
您好!測試可以使用,非常感謝您的熱心幫忙。
  
再您請教
若在C欄位(日期欄位),他輸入的日期非今日日期,有可能輸入的日期為昨日或是大前天。
是否可讓VBA判讀,以C欄位最後欄位出現的日期(有可能輸入的日期為昨日或是大前天)去判讀,highlight "帳面異常"

如今日為 2020/11/10 ,在C欄位上最新輸入的日期為 2020/11/9 或是 2020/11/8
Just do it.

TOP

不明白! 請列出問題,模擬要求結果。

TOP

回復 4# ikboy


Dear ikboy,

如下圖,再麻煩您了

V2.jpg
2020-11-10 11:58



Excel_VBA-帳面錯誤-存檔時會提出警告畫面 V2.rar (14.53 KB)
Just do it.

TOP

  1. Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  2. Dim d As Date, a, b, r&, c&, t As Boolean
  3. r = [c1].CurrentRegion.Rows.Count - 1
  4. c = Columns("aj").Column
  5. a = Cells(2, 1).Resize(r, c).Value
  6. b = Cells(2, "aq").Resize(r).Value
  7. d = Application.Max(Cells(2, 3).Resize(r))
  8. For i = 1 To UBound(a)
  9.     If a(i, c - 1) > a(i, c) Then b(i, 1) = "帳面錯誤": If d = a(i, 3) Then t = True
  10. Next
  11. Cells(2, "aq").Resize(r) = b
  12. If t Then MsgBox "請注意" & d & "帳面錯誤"
  13. End Sub
複製代碼

TOP

回復 5# jsc0518


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim R, D, i&, K%
R = Cells(Rows.Count, "C").End(xlUp).Row
D = Cells(R, "c")
If Not IsDate(D) Then Exit Sub
For i = R To 2 Step -1
    If Cells(i, "c") <> D Then Exit For
    If Cells(i, "AQ") = "帳面錯誤" Then K = 1: Exit For
Next i
If K > 0 Then MsgBox "請注意!! " & D & " 帳面錯誤 "
End Sub

TOP

回復 7# 准提部林
Dear 准提部林,
感謝您的方法,TEST OK。
Thank you.
Just do it.

TOP

回復 6# ikboy

Dear ikboy,
感謝您熱心指導歐!
Thank you so much.
Just do it.

TOP

回復 7# 准提部林

版主您好
不好意思,可否麻煩您說明語法的意思
感謝您

TOP

        靜思自在 : 【時間成就一切】時間可以造就人格,可以成就事業,也可以儲積功德。
返回列表 上一主題