Consulting

Results 1 to 3 of 3

Thread: MsgBox is making the code hang

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    410
    Location

    MsgBox is making the code hang

    I have a worksheet change event that fires if a specific condition exists.
    [cells to the left are blank ]
    I want to tell the User to post data to those cells first, but my MsgBox is making the code hang.
    Any ideas.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Offset(-4, 0).Value = "" Or Target.Offset(-7, 0).Value = "" Then
        Target.Value = ""
        MsgBox " POST THE DOC FIRST "
        Exit Sub
    End If
    End Sub

  2. #2
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    193
    Location
    1) It will raise the run-time error when target row is smaller than 7
    2) When you change the value with Change event, you need to disable events, otherwise infinite event loop...
    Private Sub Worksheet_Change(ByVal Target As Range)
        With Target
            If .Row < 7 Then Exit Sub
            Application.EnableEvents = False
            If .Offset(-4, 0).Value = "" Or .Offset(-7, 0).Value = "" Then
                .Value = ""
                MsgBox " POST THE DOC FIRST "
            End If
            Application.EnableEvents = True
        End With
    End Sub
    Last edited by jindon; 05-25-2025 at 10:32 PM.

  3. #3
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    410
    Location
    Thanks jindon

    Forgot all about disabling events scenarios.
    That solved the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •