Update
I was able to get the code to run, but I still think i'm doing something wrong. It's not copying and pasting the right value, just pasting the original value over and over. New code below.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("X2")
Dim rb As Worksheet
Set rb = ThisWorkbook.Worksheets("RB_Cur")
Dim dp As Worksheet
Set dp = ThisWorkbook.Worksheets("Dispatch Plan")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
dp.Range("V4:V16").ClearContents
With rb.Range("E1:AA1000" & rb.Range("E" & Rows.Count).End(xlUp).Row)
.AutoFilter Field:=1, Criteria1:=dp.Range("X2").Value
.AutoFilter Field:=7, Criteria1:=dp.Range("W1").Value
If .SpecialCells(xlCellTypeVisible).Count > 0 Then
rb.Range("AA1").Offset(1).Resize(10, 1).Copy
dp.Range("V4").PasteSpecial xlPasteValues
End If
End With
End If
End Sub
Original Post
I am trying to create a table where someone can input a route number into a cell and it will populate with the stops on that route. However, i'm running into some trouble getting the stop list to populate when the cell value is changed.
Picture example: When the highlighted cell (X2) is changed, I have a code that searches it in a different sheet and copies over the stops into the Stop column. The code to lookup and copy works, but it doesn't change when the number is changed.
RouteLookup code (this works)
Sub RouteLookup()
Dim rb As Worksheet
Set rb = ThisWorkbook.Worksheets("RB_Cur")
Dim dp As Worksheet
Set dp = ThisWorkbook.Worksheets("Dispatch Plan")
With rb.Range("E1:AA1000" & rb.Range("E" & Rows.Count).End(xlUp).Row)
.AutoFilter Field:=1, Criteria1:=dp.Range("X2").Value, Criteria1:="EARLY BIRD"
If .SpecialCells(xlVisible).Count > 0 Then
rb.Range("AA1").Offset(1).Resize(10, 1).Copy Destination:=dp.Range("V4")
End If
.AutoFilter Field:=1
End With
End Sub
Worksheet SelectionChange code - Whenever I try to test it, it pops up a box asking me to select the macro. I don't understand why because the code calls the RouteLookup already.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("X2")) Is Nothing Then
Application.EnableEvents = False
Call RouteLookup
Application.EnableEvents = True
End If
End Sub