Run VBA macro when condition is met

excel, vba

Solution

Add this as code for your Worksheet:

Private Sub Worksheet_Change(ByVal Target As Range)
    If (Range("A1") = "correct!") Then
        ''# do your stuff here
    End If
End Sub

`Worksheet_Change` is called whenever something is changed. As it looks like `A1` is calculated, you can not check for `Target` in that case but check for the value of the cell.

Problem

I'm creating a spreadsheet to train my numerical skills. Now, I use VBA macros to generate a new problem once the current one has been correctly solved. To do so, I still have to press a button in the worksheet, which costs time and is annoying. Is there a way that I can execute a macro when a certain condition is met? for instance: ``` if A1 = "correct!" then <run macro> else <do nothing> ``` Also, let me know if you're interested in the spreadsheet, I'd be happy to share it with you guys. best, Pieter

Original source