drop-down list selection in a cell activating a macro when changed but whose location can vary

42 Views Asked by At

I have a worksheet with 3 sheets, the user selects names from the first sheet and generates an output form in the second sheet with a drop down list of selected names. However, the location of the drop-down list can vary depending on the total number of names selected. This precludes using the Worksheet.Change event in the sheet object I think because you have to specify the cells a priori. I'd like to be able set up targets AFTER locating the cells for the drop-down variables.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Dim nSearchRow As Integer

' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
  Set KeyCells = Range("A4,A5,A8,A9,A12,A13,A15,A16,C4,C5,C8,C9,C12,C13,C15,C16")

 n = 1
 
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
        Is Nothing Then
'search for name inlist and move to next session
        If Not IsEmpty(Target.Value) = True Then
            cSearchText = Target.Value
                On Error Resume Next
                    nSearchRow = WorksheetFunction.Match(cSearchText, Worksheets("Available").Range("B1:B50"), 0)
                'If you find a matching value, indicate success by setting bln to true and exit the loop;
                'otherwise, continue searching until you reach the end of the workbook.
                If Err.Number <> 0 Then
                    MsgBox "Call Help desk"
                Else
                    Worksheets("Available").Range("B" & nSearchRow).Copy
                    Worksheets("Available").Cells(nSearchRow, 3).PasteSpecial Paste:=xlPasteValues
                    Worksheets("Available").Range("B" & nSearchRow).ClearContents
                End If
            End If
End If

End Sub
0

There are 0 best solutions below