Attached ORDER file has DropLists of Products and Suppliers in Cols B - C
Attached Supplier file contain PRODUCT and COST columns.(Real folder has several different supplier files).
User selects a Product and then a Supplier.
Worksheet change finds the relevant Supplier file, the Product selected in the ORDER file, and copies the Cost from Col B of the SUPPLIER into Col D of the ORDER file.
Option Explicit
Dim a As Long
Dim KeyCells As Range
Dim wkb As Workbook
Dim wks As Worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
'Find last row with data in Col C
With ActiveSheet
a = .Cells(.Rows.Count, "C").End(xlUp).Row
'Set Cells that will cause an alert when they are changed.
Set KeyCells = .Range("C2:C" & a)
'Exit Macro if more than one cell is chosen
If Target.Count > 1 Then Exit Sub
'Run Macro if one cell in Col C changes
If Not Application.Intersect(KeyCells, .Range(Target.Address)) Is Nothing Then
'Set workbook "selected" in the target cell, and add ".xls" as extension ***throws a "RuntIme Error 424 - Object Required"***
Set wkb = Sheet1.Range(Target.Address) & ".xls"
'Set sheet 1 of that workbook as the sheet to check
Set wks = wkb.Sheet1
'Find the relevant file for whichever Supplier is selected in the Target cell, find the Product in the Supplier Col A matching Col B on the Target row, copy the value from Supplier Col B into Col D of the Target row in the ORDERS file
.Range(Target.Address).Offset(0, 1) = Application.WorksheetFunction.Index(wks.Range("B:B"), Application.WorksheetFunction.Match(.Range(Target.Address).Offset(0, -1), wks.Range("A:A"), 0))
End If
End With
End Sub
All solutions, suggestions and alternatives welcome as ever.
Ochimus
Bookmarks