+ Reply to Thread
Results 1 to 12 of 12

Change Event based on two other cell values

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Change Event based on two other cell values

    myColumnOne = Range("NPN").Column 'this is column B, NPN is a Named Range of B1
    myColumnTwo = Range("NPCH").Column 'this is column E, NPCH is a Named Range of E1

    using these variables I want to say

    When data is entered into any cell in myColumnOne
    first check to see if this same data already exists in myColumnOne
    if it does then check to see if in the row where the data already exists, if the corresponding cell in myColumnTwo ISBLANK then MsgBox

    if the corresponding cell in myColumnTwo is not blank, allow the data to be entered.


    Example:
    Col B….Col E
    ABC…..xxx
    XZY…..

    It will allow ABC to be entered because Column E is populated
    It will not allow XYZ to be entered because Column E is blank, rather it will give a MsgBox alerting the user that the data already exists.

    Thank you, I've never done a Change Event before

    added & edited:

    I should also add that the data being entered into myColumnOne will be entered into the last cell (first blank) in the column.
    And that data may exist multiple times in the preceding cells, so each instance must be checked.

    I'm thinking that a For Next Loop to check the preceding cells for the data and a Offset to check the corresponding cell in myColumnTwo is what's needed. But then if it was some kind of a Do Until Loop, it would stop when it found a blank cell in myColumnTwo.

    Lastly, if it's even possible, it would find the existing data with the blank cell much faster if it searched from the bottom up instead of from row 1 down.
    Last edited by carsto; 05-09-2007 at 12:52 AM. Reason: added info

  2. #2
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686
    Okay, I have something that sort of works (Half the time!) but needs fixed

    Sub ExistingDataSearch()
    'if the new entry into the last cell in column B is already in Column B
    'then look in the corresponding row column E for a blank cell
    'if E is blank, don't allow the new entry
    'if E is not blank, allow the new entry
    
    NewEntry = ActiveCell.Value    'eventually this will be the ChangeEvent Cell
    iOrigLastRow = ActiveSheet.Range("B65536").End(xlUp).Row - 1
        
    If Application.WorksheetFunction.CountIf(Range("B:B"), NewEntry) > 1 Then
    
        For r = 2 To iOrigLastRow 'will iOrigLastRow To 2 Step -1 work to search from the bottom up?
    
             Set found = Range(Cells(2, 2), Cells(iOrigLastRow, 2)).Find(NewEntry, LookIn:=xlValues)
             
    '         If Not found Is Nothing Then
                firstaddress = found.Address
                Do
                   If found.Row <> r Then
                         If IsEmpty(found.Offset(0, 3)) Then
                            MsgBox ("you cannot enter data")
                            Exit Sub      'AND DON'T allow entry into cell
                        End If
                   End If
                   
                   Set found = Range(Cells(2, 2), Cells(iOrigLastRow, 2)).FindNext(found)
                Loop While found.Address <> firstaddress
    '            Loop While Not found Is Nothing And found.Address <> firstAddress
    '         End If
          
          Next
    End If
    End Sub
    It finds them, checks column E and messages & Exits Sub if the blank is found - WORKS GREAT *** but now I need to make it disallow the entry
    If a corresponding blank cell is never found it Loops CONTINUOUSLY
    -it doesn't seem to ever think the found.Address = firstaddress

    I have no idea what I'm doing, so feel free to majorly critique this mess!

    Once I get it working, I stilll need to make it into a Change Event, it doesn't have to happen every time a cell in Column B is changed just when the newest entry, which will be in the last row (first open) is entered.

  3. #3
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    try this and see if it does what you need.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 2 Then
    Dim LastUsedRow As Long
    Dim NewEntry As String
    Dim BlankFound As Boolean
    Dim FNDBlnkRWS As String
    Dim i As Long ' Used for stepping thorugh rows.
    BlankFound = False 'set flag false to start
    
    FNDBlnkRWS = ""
    NewEntry = Target.Value
    
            If NewEntry <> "" Then
            
                LastUsedRow = Sheets(1).Range("B65336").End(xlUp).Row
                
                For i = LastUsedRow - 1 To 1 Step -1
                        If Cells(i, 2).Value = NewEntry Then
                        
                                    If Cells(i, 5).Value = vbNullString Then
                                           FNDBlnkRWS = FNDBlnkRWS & i & " , " ' records corresponding row numbers with blanks
                                            
                                            BlankFound = True
                                        
                                    'Exit For ' optional if you don't want to search for mulitple corresponding blanks in col E
                                    Else
                                                          
                                    End If
                          
                        End If
                 Next i
                 
                 
                                 If BlankFound = True Then
                                        Target.Clear ' clears entry in column be if another exists with corresponding blank
                                    
                                        MsgBox "Existing Blank cell in Row/s  " _
                                        & Trim(Left(FNDBlnkRWS, Len(FNDBlnkRWS) - 2))
                                                                  
                                                            
                                
                                 End If
            End If
    End If
    
    End Sub
    VBA - The Power Behind the Grid

    Posting a sample of your workbook makes it easier to look at the Issue.

  4. #4
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686
    Thank Steve, exactly what I needed

    however the
    If Cells(i, 2).Value = NewEntry Then
    appears to be case sensitive,
    is it possible to change that?

    ABC123 matches ABC123
    abc123 doesn't match ABC123

    PS. how do you mark a post Resolved?

  5. #5
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    you can change:

     If Cells(i, 2).Value = NewEntry Then
    to:
     If Ucase(Cells(i, 2).Value) = Ucase(NewEntry) Then
    you mark resolved using the thread tools menu

  6. #6
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686
    Thanks!

    as for Marking Resolved, the only options I have under Thread Tools is:
    Show Printable Version
    Email this Page
    Unsuscribe from this Thread
    Add a Poll to this Thread
    Admin Tools - Delete Thread

+ 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