Try this.
For the B2 Data validation list formula use this.
=INDIRECT("DetailA")
And this for C2
=INDIRECT("DetailB")
This will use two named ranges as their list ranges
The code below will update the DetailA and DetailB named ranges when you make a change to A2.
You could also put the lists on other sheets and edit the code to those sheet names and ranges. When the form-sheet is activated, the named ranges are updated to reflect any changes the user may have made.
Private Sub Worksheet_Activate()
Update_Lists
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) = "A2" Then
Range("B2,C2").ClearContents
If Target <> "" Then Update_Lists
End If
End Sub
Private Sub Update_Lists()
Dim selected_company As String, r As Long, rc As Long
If Range("A2").Value = "" Then Exit Sub
With Sheets("Sheet1") 'Company list sheet
selected_company = Application.WorksheetFunction.VLookup(Range("A2"), .Range("F:G"), 2, False)
End With
With Sheets("Sheet1") 'Product\Detail sheet
'sort on company
.Range("I:K").Sort Key1:=.Range("I1"), Order1:=xlAscending, Header:=xlYes, MatchCase:=False
'First row of Company
r = Application.Match(selected_company, .Range("I:I"), 0)
'Count of company entries
rc = Application.CountIf(.Range("I:I"), selected_company)
'Named ranges
ThisWorkbook.Names.Add Name:="DetailA", RefersTo:="=" & .Range("J" & r).Resize(rc).Address(1, 1, xlA1, 1)
ThisWorkbook.Names.Add Name:="DetailB", RefersTo:="=" & .Range("K" & r).Resize(rc).Address(1, 1, xlA1, 1)
End With
End Sub
Bookmarks