Forum Discussion

legendsaud's avatar
legendsaud
Copper Contributor
Dec 15, 2024
Solved

Can you Automatically Update Formulas and Data Validation Between 2 cells in Excel Using VBA

In Excel, I have the following formula in cell D14: =LET(   lookupValue, $D$13,   Name, VLOOKUP(lookupValue, Cust_Tbl, 2, 0),   IF(lookupValue<>"", IF(Name<>"", Name, ""), "") ) This formul...
  • legendsaud's avatar
    legendsaud
    Dec 31, 2024

    Thanks, and sorry for the late reply. I like your approach and it will probably work. However, following is the code that works and requires lesser steps:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Application.EnableEvents = False
        On Error GoTo ExitHandler
    
        If Not Intersect(Target, Range("D13")) Is Nothing Then
            ' Set formula in D14 when D13 changes
            Range("D14").Formula = "=LET(lookupValue,$D$13,Name,VLOOKUP(lookupValue,Cust_Tbl,2,0),IF(lookupValue<>"""",IF(Name<>"""",Name,""""),""""))"
        ElseIf Not Intersect(Target, Range("D14")) Is Nothing Then
            ' Set formula in D13 when D14 changes
            Range("D13").Formula = "=IF(D14<>"""",XLOOKUP(D14,Cust_Tbl[Customer Name],Cust_Tbl[Contact]),"""")"
    
        End If
    
    ExitHandler:
        Application.EnableEvents = True
    End Sub

    It turns out data validation didn't need to be explicitly declared everytime.

Resources