Previously I have created an Excel sheet which obtains data from a master table and perform Index & Match formula on selected columns based on what was keyed in the first column.
The code I came up with was:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a4:a9999")) Is Nothing Then
With Me.Range("b4:b9999")
.Formula = "=IF(ISNA(INDEX(MasterEntry,MATCH(A4,table[Project No],FALSE),2)),"""",INDEX(MasterEntry,MATCH(A4,table[Project No],FALSE),2))"
.Value = .Value
End With
With Me.Range("c4:c9999")
.Formula = "=IF(ISNA(INDEX(MasterEntry,MATCH(A4,table[Project No],FALSE),3)),"""",INDEX(MasterEntry,MATCH(A4,table[Project No],FALSE),3))"
.Value = .Value
End With
End If
End Sub
How it works is when the first column detects any changes within the range a4 to a9999, it will change all the values in column b and c based on the range as well.
Currently I'm trying to modify it so that data will only change based on which row I select. For example, if I change the data in A5 only, I want B5 and C5 only to perform the code and the others remain unchanged.
Is it possible to use worksheet_change to perform this or do I need another approach entirely?
As an extension to the question, what happens if I want the same function to work in a table? The code used for table version is such:
Private Sub worksheet_change(ByVal target As Range)
If Not Intersect(target, Me.ListObjects("ProjectEntry").ListColumns("Asset No").DataBodyRange) Is Nothing Then
With Me.Range("ProjectEntry[Description]")
.Formula = "=IF(ISNA(INDEX(DieMaster,MATCH(B4,DieMaster[Asset No],FALSE),2)),"""",INDEX(DieMaster,MATCH(B4,DieMaster[Asset No],FALSE),2))"
.Value = .Value
End With
With Me.Range("ProjectEntry[Preventive Stroke]")
.Formula = "=IF(ISNA(INDEX(DieMaster,MATCH(B4,DieMaster[Asset No],FALSE),3)),"""",INDEX(DieMaster,MATCH(B4,DieMaster[Asset No],FALSE),3))"
.Value = .Value
End With
End If
End Sub
How would I code in the function if range is a table?