Thank you for reaching out. Please find the answer below.
In Excel VBA, there is no built-in method to directly detect if Solver is currently running. Solver runs synchronously, meaning that when you invoke it with SolverSolve, VBA execution pauses until Solver finishes. There is no property or event you can check mid-run. To create a robust solution that works whether Solver is run manually or via VBA, you can combine several strategies:
1. Check Solver Activity
Create a named cell in your workbook, for example SolverStatus. Before starting Solver, set the value to "Running". After Solver completes, set it back to "Idle". In any of your event-driven or calculation-sensitive code, check this flag:
If Range("SolverStatus").Value <> "Running" Then
' Safe to run your logic
End If
2. Manual Solver Runs
Before running Solver manually, set:
Range("SolverStatus").Value = "Running"
Range("SolverStatus").Value = "Idle"
Sub PrepareSolverRun()
Range("SolverStatus").Value = "Running"
MsgBox "Solver status set to Running. Please run Solver manually now."
End Sub
3. Passive Detection via Cell Monitoring (Optional)
If Solver changes a known cell, you can monitor that cell on a timer and update the status flag when it changes:
Public LastValue As Variant
Sub StartMonitoring()
LastValue = Range("TargetCell").Value
Application.OnTime Now + TimeValue("00:00:01"), "CheckSolverActivity"
End Sub
Sub CheckSolverActivity()
If Range("TargetCell").Value <> LastValue Then
Range("SolverStatus").Value = "Running"
LastValue = Range("TargetCell").Value
End If
Application.OnTime Now + TimeValue("00:00:01"), "CheckSolverActivity"
End Sub
This method is heuristic and works best if Solver is expected to update a specific cell.
4. VBA-Controlled Solver Runs
If you run Solver from VBA, you can also use a Boolean variable along with the named cell:
Public SolverWasJustRun As Boolean
Sub RunSolver()
SolverWasJustRun = True
Range("SolverStatus").Value = "Running"
SolverSolve UserFinish:=True
SolverWasJustRun = False
Range("SolverStatus").Value = "Idle"
End Sub
By using a named cell flag for manual runs, a Boolean flag for VBA-controlled runs, and optional passive detection for known cell changes, you can make your code skip execution safely during Solver’s activity in all scenarios.
Let us know if the issue persists after following these steps. We’ll be happy to assist further if needed.