0

I am a fresher in VBA and want to do a VBA as below conditions, could anyone help me? Please……

  1. Cell J2 us set to be today()
  2. Cell J4 can be set another date by user
  3. Cell L4 set a formula =NETWORKDAY(J2-J4)-1, to calculate the exact days between 2 dates in J2 & J4
  4. if the outcome in L4 is <4, user could continue to fill in the form;
  5. Otherwise, 2 msg box pop up in sequence
    • msgbox : 'Application takes at least 4-7 workings days';
    • msgbox : 'Please pick another date'
  6. return to Cell J4 which will be cleared as well
  7. User should input the date in J4 again

here is my code, but it doesn't work:

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False

If Range("L4") >= 4 Then Exit Sub

Else

msg = MsgBox("Application takes at least 4-7 working days", vbOKOnly, "Reminder")

Range("J4").Select

Selection.ClearContents

msg = MsgBox("Please choose another date", vbOKOnly)

End If

End sub`

enter image description here

Darren Bartrup-Cook
  • 18,362
  • 1
  • 23
  • 45
Joffee
  • 5
  • 3

2 Answers2

0

Before End Sub you have to re-enable events like this

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False

If Range("L4") >= 4 Then
Application.EnableEvents = True
Exit Sub
Else
    msg = MsgBox("Application takes at least 4-7 working days", vbOKOnly, "Reminder")
    Range("J4").Select
    Selection.ClearContents
    msg = MsgBox("Please choose another date", vbOKOnly)
End If
Application.EnableEvents = True
End sub

or another implementation if only disable events if date is not good.

Private Sub Worksheet_Change(ByVal Target As Range)



If Range("L4") >= 4 Then
Exit Sub
Else
    Application.EnableEvents = False
    msg = MsgBox("Application takes at least 4-7 working days", vbOKOnly, "Reminder")
    Range("J4").Select
    Selection.ClearContents
    msg = MsgBox("Please choose another date", vbOKOnly)
End If
Application.EnableEvents = True
End sub

Sidenote: Gives more information about the error parameter (what is displayed, which line, relevant values etc.) this result more precise and faster answers mainly if more complex codes.

Black cat
  • 1,056
  • 1
  • 2
  • 11
0

First check to see if the Worksheet_Change Target is the date cell that the user is updating (J4). If it is, check L4 and pop your message box. Lastly turn events back on so it can trigger again next time there is a change.

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Not Intersect(Range("J4"), Target) Is Nothing Then:
        If Range("L4").Value < 4 Then
             msg = MsgBox("Application takes at least 4-7 working days", vbOKOnly, "Reminder")
             Range("J4").ClearContents
             meg = MsgBox("Please choose another date", vbOKOnly)
        End If
    End If
    Application.EnableEvents = True
End Sub
JNevill
  • 46,980
  • 4
  • 38
  • 63
  • Thanks for your efforts. However, it doesn't work. No pop window at all. No event activated. Might the problem be that NETWORKDAYS formula set in L4? I dun know.....ToT – Joffee Jul 15 '23 at 05:13
  • Sorry for not responding sooner. I suspect that question was right-on. I've updated the logic to check the correct cell. – JNevill Jul 17 '23 at 13:04
  • Thousands thanks for your assistance. It works !! I'm much appreciated for your help!! U've solved a big big problem for me ! – Joffee Jul 21 '23 at 08:22