Forum Discussion

LouisWood's avatar
LouisWood
Copper Contributor
Jul 13, 2022

Run a macro only once

Hello,

I want a macro to run when the value in a cell meets a certain criteria but I want it to only run once, not every second or two for as long as the condition is met. So in addition to checking the value of the target cell, I also check that the value in another cell is not "Alerted." In my macro, I set the value of that second cell to "Alerted" which should prevent the formula from calling the function again.

It works perfectly when run in the debugger, but when called from a sheet it fails on the line that should set the value of the second cell to "Alerted."

I have tried using
Application.EnableEvents = False
and
Application.Calculation = False

but it still fails. When I set a break point and then step through the code, when it gets to the line that should set the value execution jumps back to the first line in the macro as if it is called again (which is why I thought about turning automatic calculation off).

 

This is the function:

=IF(AND(AT1139<>"",AZ1139<>"Alerted"),PlayWAV(),"")

 

This is the macro (including some commented out lines that did not help):

Function PlayWAV()
Application.EnableEvents = False
'Application.Calculation = xlManual
'Application.Calculation = xlCalculationManual
Application.Calculation = XlCalculation.xlCalculationManual

 

Range("$AZ$1139").Value = "Alerted"    'This is the line that fails

'Application.Calculation = xlAutomatic
'Application.Calculation = xlCalculationAutomatic
Application.Calculation = XlCalculation.xlCalculationAutomatic
Application.EnableEvents = True


WAVFile = "C:\Users\Louis Wood\OneDrive\Documents\Media\Sounds\Custom\PriceAlert.wav"
'Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)

End Function

 

8 Replies

  • LouisWood 

    A function in a cell formula cannot change the value of another cell.

    Will the user change the value of AT1139 or does it contain a formula?

    • LouisWood's avatar
      LouisWood
      Copper Contributor

      HansVogelaarYes, AT1139 is normally empty and I want the function to run when another process puts text in it.

       

Resources