How it works
Application.Volatile
Some things that occur automatically in Excel auto-trigger cell formulas to recalculate and other things do not. Fortunately your SQL update triggers recalculation
By default UserDefinedFunctions are only recalculated when any of the function's arguments change. Adding
Application.Volatile makes it recalculate whenever calculation occurs in any cells on the worksheet
Application.CommandBars("Standard").Controls("&Undo").List(1)
Here though, you do not want the function to change the value in a cell whenever the worksheet recalculates - it does that in the background all the time. You want the value to change ONLY when SQL auto-refresh occurs.
Application.CommandBars("Standard").Controls("&Undo").List(1) provides a string telling us what Excel did last (when you click once on Undo this is the action that is undone).
Static variable
Defining a variable as Static retains its value after the function has run - here the old time value must be retained until auto-refresh occurs
Summary
1. Recalculate the cell
2. Was last action auto-refresh?
3. If YES - leave things as they are
4. If NO - replace recalculated value with the previous cell value
5. Save current value as OldValue for next time
A tiny UDF that has a lot going on!
Note of caution
Fortunately your workbook is not also auto-refreshing from an Access Database. Hopefully you are not using Pivot tables either.
Otherwise more sophistication is required in idenyifying the correct auto-refresh
Application.CommandBars("Standard").Controls("&Undo").List(1) returns string
"Refresh All" for all 3 which is rather unhelpful.