Trigger macro if drop down selection is changed.

Hi, I have a form where the user selects a chemical from the drop down (B5), then enters a date (B7).  Based on the chemical and date entered, the form populates an expiration date on the form.  My issue is that if you change the drop down after entering the date, the expiration date doesn't get updated since the formula is triggered off the date field only.  How can I clear the entered date (B7) if the drop down selection changes (B5).  I tried the following formula:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$B$5" Then
        Range("B7").ClearContents
    End If

End Sub

However, once you enter the date, it returns you to the drop down cell, so Excel see this as a selection change and runs the code to clear the form.  I need it to only change if you make a different selection from the drop down, not when the cell is selected.

Thanks,

Scott

Answer
Answer

Use the Worksheet_Change event instead of the Worksheet_SelectionChange event.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$5" Then
        Range("B7").ClearContents
    End If

End Sub

---
Best wishes, HansV
https://www.eileenslounge.com

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 344 Applies to: