+ Reply to Thread
Results 1 to 8 of 8

loop to stop and wait for cell value change.

Hybrid View

ajwhipple loop to stop and wait for... 09-28-2007, 05:35 PM
ajwhipple Well, I've partially found my... 09-28-2007, 07:55 PM
rylo HI Try Private Sub... 09-28-2007, 09:31 PM
ajwhipple Sorry for the late reply. I... 10-02-2007, 06:52 PM
shg Perhaps this: Private Sub... 10-02-2007, 07:09 PM
  1. #1
    Registered User
    Join Date
    09-25-2007
    Posts
    13

    loop to stop and wait for cell value change.

    I'm very new to this, but I have a simple macro that is working largely as intended. I want it to look at a cell, and give the user an error message if the cell value is less than or equal to a referenced cell. I want it to keep checking the cell, but I need to give the user an opportunity to change the value before each check. Can I set up some trigger that activates when the user enters data into the cell?

    This is my code:

    Sub proControlCheck()
        Range("B2").Select
        Do Until Selection.Value > "A2"
            If Selection.Value <= Range("A2") Then
            MsgBox "The High Control Value is less than the Low Control."
            Range("B2").Select
            End If
        Loop
    End Sub
    I appreciate any help.

  2. #2
    Registered User
    Join Date
    09-25-2007
    Posts
    13
    Well, I've partially found my solution. I modified the code as follows:

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
     If Target.Address = "$B$2" Then
        Range("B2").Select
            Do Until Selection.Value > "A2" Or Selection.Value = ""
                If Selection.Value <= Range("A2") Then
                MsgBox "The High Control Value is less than the Low Control."
                Selection.ClearContents
                End If
            Loop
     End If
    End Sub
    This works great as long as I keep entering values into cellB2 that cause the message box to appear. Once I enter a value that is greater than the value in cell A2, then spreadsheet accepts the value, then after a few seconds, Excel freezes.

    Any ideas why?

  3. #3
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    HI

    Try

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
     If Target.Address = "$B$2" Then
            Do Until Target.Value > Range("A2").Value Or Selection.Value = ""
                MsgBox "The High Control Value is less than the Low Control."
                Target.ClearContents
            Loop
     End If
    End Sub

    rylo

  4. #4
    Registered User
    Join Date
    09-25-2007
    Posts
    13
    Sorry for the late reply. I tried your suggestion, and it didn't work. I thought that it might be because I have Excel set to automatically move the selection after pressing enter. When I disabled that option, it works fine.

    The reason I initially have the line in there to select range B2 was to make sure it was looking at the correct cell, even if the selected cell is not B2. I replaced that line, and now it works great. My final macro code is:

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
     If Target.Address = "$B$2" Then
        Range("B2").Select
            Do Until Target.Value > Range("A2").Value Or Selection.Value = ""
                MsgBox "The High Control Value is less than the Low Control."
                Target.ClearContents
            Loop
     End If
    End Sub
    Thanks for your help!
    Last edited by ajwhipple; 10-02-2007 at 06:59 PM.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Perhaps this:
    Private Sub Worksheet_Change1(ByVal Target As Range)
        If Target.Address <> "$B$2" Then Exit Sub
        If Range("B2") >= Range("A2") Then Exit Sub
        
        Range("B2").Select
        MsgBox "The High Control Value is less than the Low Control."
        Application.EnableEvents = False
        Range("B2").ClearContents
        Application.EnableEvents = True
    End Sub
    Note that this goes in the Worksheet module, not the Workbook module.

    If you want B2 fixed before the user is allowed to change anything else, then,
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Range("B2") >= Range("A2") Then Exit Sub
        Application.Undo
        
        Range("B2").Select
        MsgBox "The High Control Value is less than the Low Control."
        Application.EnableEvents = False
        Range("B2").ClearContents
        Application.EnableEvents = True
    End Sub
    Also be aware that when the change event changes the worksheet (like clearing the contents of B2), Excel flushes the Undo buffer, which may aggravate users.
    Last edited by shg; 10-02-2007 at 07:12 PM.

  6. #6
    Registered User
    Join Date
    09-25-2007
    Posts
    13
    I do want the users to correct the value before they move on, so I tried your second code set. Unfortunately, it wouldn't allow me to enter a new value after it cleared the cell, because the cell was empty, which triggered the message box. I changed the code as follows:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Range("B2") >= Range("A2") Or Target.Value = "" Then Exit Sub
        Application.Undo
        
        Range("B2").Select
        MsgBox "The High Control Value is less than the Low Control."
        Application.EnableEvents = False
        Range("B2").ClearContents
        Application.EnableEvents = True
    End Sub
    Now it ends the macro if the cell is empty too. Unfortunately, it also allows the user to leave the cell blank, which isn't exactly desireable either. There are further operations in the sheet that will require values for both cells, so the omission would be caught later, but it would be nice to catch it in the beginning too.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1