Worksheet Change Event

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
585
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the worksheet change event code below that allows me to select any cell in my table column called "Description (Excluding Job List EQPT)" (Column I) and add as many items as I want from a drop-down list. The drop-down list is populated from a table on another worksheet called "Drops". That table name is called "tblDescription". As a user selects an item or items from the drop-down list, each one is added in the cell to the previous selection with a comma separating each selections. The problem I have is with the message box that pops up every time once you go to select the second item you want to add to the cell in that column. If each item I select (called "My_Value" and "My_HValue" in the code) is already in the drop-down list, I don't want that message box to appear. I only want it to appear if I am making an entry in that cell at any point that is something not found in that table that is referenced by the drop-down list, because that would be when I want the option to automatically add it to that table on the "Drops" worksheet. Basically, any entry that is not equal to the "My_Value" or "My_HValue" in the code should prompt that pop-up message box allowing me the option to add the item to the said table..

For example:

If my table on the "Drops" worksheet has the following entries in a single column table,

Red
Blue
Green
Yellow
Orange
Brown

If I were to select Red, Blue and Brown, the cell entry should look like "Red, Blue, Brown" and I should not get that pop-up message box. If I decided to make my first entry something that is not on the table, for instance "Purple" and then proceed to select Blue and Brown, I would like the message box to appear and ask if I want to add "Purple" to my drop-down list. Instead, as the code is written right now if I selected yes to the prompt, the entry "Purple, Blue, Brown" would be added to the table that the drop-down list references. Right now, my users just always have to keep selecting "No" every time they select additional items from the drop-down list.

Any assistance would be greatly appreciated.

VBA Code:
Option Explicit
Dim Oldvalue As String
Dim Newvalue As String
Dim oldAddress As String

Private Sub Worksheet_Change(ByVal Target As Range)

Dim sSheetName As String
Dim R As Long
Dim cl As Range
Dim ws As Worksheet
Dim str As String
Dim i As Integer
Dim rngDV As Range
Dim rng As Range
Dim lCol As Long
Dim myRsp As Long
Dim My_Value As String
Dim My_HValue As String
Dim strList As String

On Error GoTo Exitsub
If Not Intersect(Target, Range("I3:I3002")) Is Nothing Then
    If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
        GoTo Exitsub
        Else: If Target.Value = "" Then GoTo Exitsub Else
            Application.EnableEvents = False
            Newvalue = Target.Value
            Application.Undo
            Oldvalue = Target.Value
            If Oldvalue = "" Then
                Target.Value = Newvalue
            Else
            If InStr(1, Oldvalue, Newvalue) = 0 Then
                Target.Value = Oldvalue & ", " & Newvalue
            Else:
                Target.Value = Oldvalue
            End If
        End If
    End If
End If

Application.EnableEvents = True

Exitsub:
    
Application.EnableEvents = True

If Target.Count > 1 Then Exit Sub

oldAddress = Target.Address

sSheetName = ActiveSheet.Name
R = ActiveCell.Row

'CODE TO ENABLE ADDING ITEMS TO DROP-DOWN LIST STARTS HERE
On Error Resume Next

Set ws = Worksheets("Drops")

If Target.Row > 1 Then
    On Error Resume Next
    Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)

    On Error GoTo 0

    If rngDV Is Nothing Then Exit Sub

    If Intersect(Target, rngDV) Is Nothing Then Exit Sub

    str = Target.Validation.Formula1
    str = Right(str, Len(str) - 1)

    On Error Resume Next
    Set rng = ws.Range(str)

    On Error GoTo 0
    If rng Is Nothing Then Exit Sub

    If Application.WorksheetFunction _
        .CountIf(rng, Target.Value) Then
        Exit Sub

Else

    My_Value = Target.Value 'Initial item selected

    My_HValue = Target.Offset(2 - Target.Row).Value 'Subsequent item(s) selected

    myRsp = MsgBox("Add '" & My_Value & "' to the '" & My_HValue & "' drop down list?", _
        vbQuestion + vbYesNo + vbDefaultButton1, _
        "New Item -- not in drop down")

    If myRsp = vbYes Then
        lCol = rng.Column
        i = ws.Cells(Rows.Count, lCol).End(xlUp).Row + 1
        ws.Cells(i, lCol).Value = Target.Value

        strList = ws.Cells(1, lCol).ListObject.Name

        With ws.ListObjects(strList).Sort
            .SortFields.Clear
            .SortFields.Add _
                Key:=Cells(2, lCol), _
                SortOn:=xlSortOnValues, _
                Order:=xlAscending
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With

        With ws.ListObjects(strList)
            .Resize .DataBodyRange.CurrentRegion
        End With

    End If

End If

End If
'CODE TO ENABLE ADDING ITEMS TO DROP-DOWN LIST STOPS HERE

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I'm still struggling with this one if anyone can offer some assistance. Thank you, SS
 
Upvote 0
Well after a couple of days banging this out, I've got something that works. Just wanted to post it here in case it benefits someone else. There is probably a lot of redundancy in here, but had to set a lot traps in the code to catch the different scenarios.

VBA Code:
Option Explicit
Dim Oldvalue As String
Dim Newvalue As String
Dim oldAddress As String

Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
Dim tb1 As ListObject
Dim matchResult As Variant
Dim temp_Target As Variant
Dim lCol As Long
Dim lCol2 As Range
Dim rngDV As Range
Dim rng As Range
Dim cell As Range
Dim cl As Range
Dim R As Long
Dim myRsp As Long
Dim n As Long
Dim tgtLen As Long
Dim i As Integer
Dim str As String
Dim sSheetName As String
Dim My_Value As String
Dim My_HValue As String
Dim strList As String
Dim searchTerm As String

Set ws = Worksheets("Drops")
Set tb1 = ws.ListObjects("tblDescription")

' Set the column reference
Set lCol2 = tb1.ListColumns("Description").DataBodyRange ' Change to your column name

' Define the target search term
searchTerm = ";"

On Error GoTo Exitsub
    If Not Intersect(Target, Range("I3:J3002")) Is Nothing Then
'        MsgBox ("Selection is in the range of the referenced target Range.")
        matchResult = Application.Match(Target, lCol2, 0)
        ' Check if a match was found
        If Not IsError(matchResult) Then
'            MsgBox "Value found in row number " & matchResult
'            MsgBox ("Selection was found in the table and was not a manual entry.")
        Else
'            MsgBox "Value not found in the table column."
'            MsgBox ("A manual entry was made in the cell that was not in the drop-down list generated from the 'Description' table.")
            GoTo Exitsub
        End If
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            GoTo Exitsub
        Else
            If Target.Value = "" Then
                GoTo Exitsub
            Else
                Application.EnableEvents = False
                Newvalue = Target.Value
                Application.Undo
                Oldvalue = Target.Value
                If Oldvalue = "" Then
                    Target.Value = Newvalue
                Else
                    If InStr(1, Oldvalue, Newvalue) = 0 Then
                        Target.Value = Oldvalue & "; " & Newvalue
                    Else
                        Target.Value = Oldvalue
                    End If
                End If
            End If
        End If
    End If

Application.EnableEvents = True

On Error GoTo Exitsub
    If Not Intersect(Target, Range("M3:M3002")) Is Nothing Then
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            GoTo Exitsub
        Else
            If Target.Value = "" Then
                GoTo Exitsub
            Else
                Application.EnableEvents = False
                Newvalue = Target.Value
                Application.Undo
                Oldvalue = Target.Value
                If Oldvalue = "" Then
                    Target.Value = Newvalue
                Else
                    If InStr(1, Oldvalue, Newvalue) = 0 Then
                        Target.Value = Oldvalue & ";" & vbNewLine & Newvalue
                    Else
                        Target.Value = Oldvalue
                    End If
                End If
            End If
        End If
    End If
    
Application.EnableEvents = True

Exitsub:

'MsgBox ("The code has taken us to 'Exitsub'")

Application.EnableEvents = True

If Target.Count > 1 Then Exit Sub

oldAddress = Target.Address

sSheetName = ActiveSheet.Name
R = ActiveCell.Row

    If ActiveSheet.Name <> "LogDetails" Then
    
        Application.EnableEvents = False
    
        'THIS CODE POPULATES Columns A through E on the "LogDetails" worksheet
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = ActiveSheet.Name & " - " & Target.Address(0, 0)
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = Oldvalue
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = Target.Value
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value = Environ("username")
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 4).Value = Now
    
        'THIS CODE POPULATES THE CELLS THAT GET SENT OUT WHEN HILLARY SENDS OUT ALL THE PO BLOCK HISTORY UPDATES
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 6).Value = ActiveSheet.Range("A" & R)  'PO# column G on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 7).Value = ActiveSheet.Range("E" & R)  'Supplier column H on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 8).Value = ActiveSheet.Range("G" & R)  'By column I on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 9).Value = ActiveSheet.Range("H" & R)  'Ship to column J on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 10).Value = ActiveSheet.Range("I" & R) 'Description(Excluding Job List EQPT) column K on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 11).Value = ActiveSheet.Range("J" & R) 'Job List Equipment column L on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 12).Value = ActiveSheet.Range("M" & R) 'DEM Job Name2 column M on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 13).Value = ActiveSheet.Range("O" & R) 'DEM Customer column N on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 14).Value = ActiveSheet.Range("P" & R) 'Notes: column O on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 15).Value = ActiveSheet.Range("Q" & R) 'Date Received: column P on LogDetails tab
    
        'ADD THE BACK LINK
        Sheets("LogDetails").Hyperlinks.Add Anchor:=Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 5), Address:="", _
        SubAddress:="'" & sSheetName & "'!" & oldAddress, TextToDisplay:=oldAddress
    
        Sheets("LogDetails").Columns("A:E").AutoFit
    
        Application.EnableEvents = True
    
    End If

'CODE TO ENABLE ADDING ITEMS TO DROP-DOWN LIST STARTS HERE
On Error Resume Next
    If Not Intersect(Target, Range("J3:J3002,M3:M3002")) Is Nothing Then
        Exit Sub
    End If
    If Target.Row > 1 Then
        On Error Resume Next
        Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
        On Error GoTo 0
        If rngDV Is Nothing Then
            Exit Sub
        End If
        If Intersect(Target, rngDV) Is Nothing Then
            Exit Sub
        End If
        str = Target.Validation.Formula1
        str = Right(str, Len(str) - 1)
        On Error Resume Next
        Set rng = ws.Range(str)
        On Error GoTo 0
        If rng Is Nothing Then
            Exit Sub
        End If
        If Application.WorksheetFunction _
            .CountIf(rng, Target.Value) Then
            Exit Sub
        Else
'            MsgBox ("The next step will look to see if there is more than one 'Description' entry in the cell by searching for the position of a semi-colon separator. If a semi-colon separator is found, then there exist more than one 'Description' in the cell.")
            n = InStrRev(Target, searchTerm)
'            MsgBox "Position of ';': " & n
            ' Check if the cell value contains the search term
            If n > 0 Then
            ' If the search term is found, do something (for example, print the cell address)
'                MsgBox ("There are multiple 'Description' entries in the selected cell.")
'                Debug.Print Target.Value
                tgtLen = Len(Target)
                temp_Target = Trim(Right(Target, tgtLen - n))    'ADDED
                matchResult = Application.Match(temp_Target, lCol2, 0)    'ADDED
                ' Check if a match was found
                If Not IsError(matchResult) Then
'                    MsgBox "Value found in row number " & matchResult
'                    MsgBox ("Selection was found in the table and was not a manual entry.")
                Else
'                    MsgBox temp_Target & "Value not found in the table column."
'                    MsgBox ("A manual entry was made in the cell that was not in the drop-down list generated from the 'Description' table.")
                    My_Value = temp_Target
                    My_HValue = Target.Offset(2 - Target.Row).Value
                    'Add new description to the description drop-down list?
                    myRsp = MsgBox("Add '" & My_Value & "' to the '" & My_HValue & "' drop down list?", _
                        vbQuestion + vbYesNo + vbDefaultButton1, _
                        "New Item -- not in drop down")
                        If myRsp = vbYes Then
                            lCol = rng.Column
                            i = ws.Cells(Rows.Count, lCol).End(xlUp).Row + 1
                            ws.Cells(i, lCol).Value = temp_Target
                            strList = ws.Cells(1, lCol).ListObject.Name
                            With ws.ListObjects(strList).Sort
                                .SortFields.Clear
                                .SortFields.Add _
                                    Key:=Cells(2, lCol), _
                                    SortOn:=xlSortOnValues, _
                                    Order:=xlAscending
                                .Header = xlYes
                                .MatchCase = False
                                .Orientation = xlTopToBottom
                                .SortMethod = xlPinYin
                                .Apply
                            End With
                            With ws.ListObjects(strList)
                                .Resize .DataBodyRange.CurrentRegion
                            End With
                        End If
                    GoTo Exitsub3    'ADDED
                End If
                GoTo Exitsub
            End If
            My_Value = Target.Value
            My_HValue = Target.Offset(2 - Target.Row).Value
            'Add new description to the description drop-down list?
            myRsp = MsgBox("Add '" & My_Value & "' to the '" & My_HValue & "' drop down list?", _
                vbQuestion + vbYesNo + vbDefaultButton1, _
                "New Item -- not in drop down")
            If myRsp = vbYes Then
                lCol = rng.Column
                i = ws.Cells(Rows.Count, lCol).End(xlUp).Row + 1
                ws.Cells(i, lCol).Value = Target.Value
                strList = ws.Cells(1, lCol).ListObject.Name
                With ws.ListObjects(strList).Sort
                    .SortFields.Clear
                    .SortFields.Add _
                        Key:=Cells(2, lCol), _
                        SortOn:=xlSortOnValues, _
                        Order:=xlAscending
                    .Header = xlYes
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
                With ws.ListObjects(strList)
                    .Resize .DataBodyRange.CurrentRegion
                End With
            End If
            GoTo Exitsub3

Exitsub2:

'            MsgBox ("The code has taken us to 'Exitsub2'")
        End If
    End If

Exitsub3:

'MsgBox ("The code has taken us to 'Exitsub3")

'CODE TO ENABLE ADDING ITEMS TO DROP-DOWN LIST STOPS HERE

'MsgBox Target.Value

'Debug.Print Target.Value

End Sub
 
Upvote 0
Sorry, had to add a "GoTo Exitsub3" line in there that I missed.

VBA Code:
Option Explicit
Dim Oldvalue As String
Dim Newvalue As String
Dim oldAddress As String

Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
Dim tb1 As ListObject
Dim matchResult As Variant
Dim temp_Target As Variant
Dim lCol As Long
Dim lCol2 As Range
Dim rngDV As Range
Dim rng As Range
Dim cell As Range
Dim cl As Range
Dim R As Long
Dim myRsp As Long
Dim n As Long
Dim tgtLen As Long
Dim i As Integer
Dim str As String
Dim sSheetName As String
Dim My_Value As String
Dim My_HValue As String
Dim strList As String
Dim searchTerm As String

Set ws = Worksheets("Drops")
Set tb1 = ws.ListObjects("tblDescription")

' Set the column reference
Set lCol2 = tb1.ListColumns("Description").DataBodyRange ' Change to your column name

' Define the target search term
searchTerm = ";"

On Error GoTo Exitsub
    If Not Intersect(Target, Range("I3:J3002")) Is Nothing Then
        MsgBox ("(1) Selection is in the range of the referenced target Range.")
        matchResult = Application.Match(Target, lCol2, 0)
        ' Check if a match was found
        If Not IsError(matchResult) Then
            MsgBox ("(2) Value found in row number " & matchResult)
            MsgBox ("(3) Selection was found in the table and was not a manual entry.")
        Else
            MsgBox ("(4) Value not found in the table column.")
            MsgBox ("(5) A manual entry was made in the cell that was not in the drop-down list generated from the 'Description' table.")
            GoTo Exitsub
        End If
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            GoTo Exitsub
        Else
            If Target.Value = "" Then
                GoTo Exitsub
            Else
                Application.EnableEvents = False
                Newvalue = Target.Value
                Application.Undo
                Oldvalue = Target.Value
                If Oldvalue = "" Then
                    Target.Value = Newvalue
                Else
                    If InStr(1, Oldvalue, Newvalue) = 0 Then
                        Target.Value = Oldvalue & "; " & Newvalue
                    Else
                        Target.Value = Oldvalue
                    End If
                End If
            End If
        End If
    End If

Application.EnableEvents = True

On Error GoTo Exitsub
    If Not Intersect(Target, Range("M3:M3002")) Is Nothing Then
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            GoTo Exitsub
        Else
            If Target.Value = "" Then
                GoTo Exitsub
            Else
                Application.EnableEvents = False
                Newvalue = Target.Value
                Application.Undo
                Oldvalue = Target.Value
                If Oldvalue = "" Then
                    Target.Value = Newvalue
                Else
                    If InStr(1, Oldvalue, Newvalue) = 0 Then
                        Target.Value = Oldvalue & ";" & vbNewLine & Newvalue
                    Else
                        Target.Value = Oldvalue
                    End If
                End If
            End If
        End If
    End If
    
Application.EnableEvents = True

Exitsub:

MsgBox ("(6) The code has taken us to 'Exitsub'")

Application.EnableEvents = True

If Target.Count > 1 Then Exit Sub

oldAddress = Target.Address

sSheetName = ActiveSheet.Name
R = ActiveCell.Row

    If ActiveSheet.Name <> "LogDetails" Then
    
        Application.EnableEvents = False
    
        'THIS CODE POPULATES Columns A through E on the "LogDetails" worksheet
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = ActiveSheet.Name & " - " & Target.Address(0, 0)
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = Oldvalue
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = Target.Value
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value = Environ("username")
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 4).Value = Now
    
        'THIS CODE POPULATES THE CELLS THAT GET SENT OUT WHEN HILLARY SENDS OUT ALL THE PO BLOCK HISTORY UPDATES
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 6).Value = ActiveSheet.Range("A" & R)  'PO# column G on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 7).Value = ActiveSheet.Range("E" & R)  'Supplier column H on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 8).Value = ActiveSheet.Range("G" & R)  'By column I on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 9).Value = ActiveSheet.Range("H" & R)  'Ship to column J on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 10).Value = ActiveSheet.Range("I" & R) 'Description(Excluding Job List EQPT) column K on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 11).Value = ActiveSheet.Range("J" & R) 'Job List Equipment column L on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 12).Value = ActiveSheet.Range("M" & R) 'DEM Job Name2 column M on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 13).Value = ActiveSheet.Range("O" & R) 'DEM Customer column N on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 14).Value = ActiveSheet.Range("P" & R) 'Notes: column O on LogDetails tab
        Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 15).Value = ActiveSheet.Range("Q" & R) 'Date Received: column P on LogDetails tab
    
        'ADD THE BACK LINK
        Sheets("LogDetails").Hyperlinks.Add Anchor:=Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 5), Address:="", _
        SubAddress:="'" & sSheetName & "'!" & oldAddress, TextToDisplay:=oldAddress
    
        Sheets("LogDetails").Columns("A:E").AutoFit
    
        Application.EnableEvents = True
    
    End If

'CODE TO ENABLE ADDING ITEMS TO DROP-DOWN LIST STARTS HERE
On Error Resume Next
    If Not Intersect(Target, Range("J3:J3002,M3:M3002")) Is Nothing Then
        Exit Sub
    End If
    If Target.Row > 1 Then
        On Error Resume Next
        Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
        On Error GoTo 0
        If rngDV Is Nothing Then
            Exit Sub
        End If
        If Intersect(Target, rngDV) Is Nothing Then
            Exit Sub
        End If
        str = Target.Validation.Formula1
        str = Right(str, Len(str) - 1)
        On Error Resume Next
        Set rng = ws.Range(str)
        On Error GoTo 0
        If rng Is Nothing Then
            Exit Sub
        End If
        If Application.WorksheetFunction _
            .CountIf(rng, Target.Value) Then
            Exit Sub
        Else
            MsgBox ("(7) The next step will look to see if there is more than one 'Description' entry in the cell by searching for the position of a semi-colon separator. If a semi-colon separator is found, then there exist more than one 'Description' in the cell.")
            n = InStrRev(Target, searchTerm)
            MsgBox "(8) Position of ';': " & n
            ' Check if the cell value contains the search term
            If n > 0 Then
            ' If the search term is found, do something (for example, print the cell address)
                MsgBox ("(9) There are multiple 'Description' entries in the selected cell.")
                GoTo Exitsub3    'ADDED
                Debug.Print Target.Value
                tgtLen = Len(Target)
                temp_Target = Trim(Right(Target, tgtLen - n))    'ADDED
                matchResult = Application.Match(temp_Target, lCol2, 0)    'ADDED
                ' Check if a match was found
                If Not IsError(matchResult) Then
                    MsgBox ("(10) Value found in row number " & matchResult)
                    MsgBox ("(11) Selection was found in the table and was not a manual entry.")
                Else
                    MsgBox ("(12) " & temp_Target & ": Value not found in the table column.")
                    MsgBox ("(13) A manual entry was made in the cell that was not in the drop-down list generated from the 'Description' table.")
                    My_Value = temp_Target
                    My_HValue = Target.Offset(2 - Target.Row).Value
                    'Add new description to the description drop-down list?
                    myRsp = MsgBox("Add '" & My_Value & "' to the '" & My_HValue & "' drop down list?", _
                        vbQuestion + vbYesNo + vbDefaultButton1, _
                        "New Item -- not in drop down")
                        If myRsp = vbYes Then
                            lCol = rng.Column
                            i = ws.Cells(Rows.Count, lCol).End(xlUp).Row + 1
                            ws.Cells(i, lCol).Value = temp_Target
                            strList = ws.Cells(1, lCol).ListObject.Name
                            With ws.ListObjects(strList).Sort
                                .SortFields.Clear
                                .SortFields.Add _
                                    Key:=Cells(2, lCol), _
                                    SortOn:=xlSortOnValues, _
                                    Order:=xlAscending
                                .Header = xlYes
                                .MatchCase = False
                                .Orientation = xlTopToBottom
                                .SortMethod = xlPinYin
                                .Apply
                            End With
                            With ws.ListObjects(strList)
                                .Resize .DataBodyRange.CurrentRegion
                            End With
                        End If
                    GoTo Exitsub3
                End If
                GoTo Exitsub
            End If
            My_Value = Target.Value
            My_HValue = Target.Offset(2 - Target.Row).Value
            'Add new description to the description drop-down list?
            myRsp = MsgBox("Add '" & My_Value & "' to the '" & My_HValue & "' drop down list?", _
                vbQuestion + vbYesNo + vbDefaultButton1, _
                "New Item -- not in drop down")
            If myRsp = vbYes Then
                lCol = rng.Column
                i = ws.Cells(Rows.Count, lCol).End(xlUp).Row + 1
                ws.Cells(i, lCol).Value = Target.Value
                strList = ws.Cells(1, lCol).ListObject.Name
                With ws.ListObjects(strList).Sort
                    .SortFields.Clear
                    .SortFields.Add _
                        Key:=Cells(2, lCol), _
                        SortOn:=xlSortOnValues, _
                        Order:=xlAscending
                    .Header = xlYes
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
                With ws.ListObjects(strList)
                    .Resize .DataBodyRange.CurrentRegion
                End With
            End If
            GoTo Exitsub3

Exitsub2:

            MsgBox ("(14) The code has taken us to 'Exitsub2'")
        End If
    End If

Exitsub3:

MsgBox ("(15) The code has taken us to 'Exitsub3")

'CODE TO ENABLE ADDING ITEMS TO DROP-DOWN LIST STOPS HERE

MsgBox ("(16) " & Target.Value)

Debug.Print Target.Value

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,810
Messages
6,181,079
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top