Just try the code as-is in the ThisWorkbook module of a new workbook.
Put the cursor in SetUpTest and run F5
Change cells in A1:A10 to trigger a calculation due to changing formula
result in B1
My debug results:
for most edit changes, including complete with Enter, or a press of any of
the 'move' keys gives debug pair-
Calculate, Change event will follow = True
Change, Calculate will follow = False
type new value, exit edit mode by clicking another cell gives debug pair-
Change, Calculate will follow = True
Calculate, Change event will follow = False
IOW seems predicate the order of the subsequent event as you asked.
But as I mentioned, the caveats for a few scenarios need to be sorted out.
I see you are handling the Windows API GetKeyState
in the SheetChange event, but in my experience that event
fires after you have released the key and the KeyState would not
indicate any key as being pressed.
In light testing I almost always found the API correctly returns the 'move'
keypress (if there was one), I assume as the change event fires before the
key is released. I'm not sure it would if the key is released very fast or
if the Calculate event will take a long time to process your other code. But
even if it does that shouldn't matter, I think. If the calc event fires
before the change event, mbFlag will be false which signifies the change
event is about to follow. I'll leave that for you to test.
Anyway, for me it seems to be working reasonably well, but sadly 'reasonably
well' does not of course mean reliably in all scenarios. Whilst I've no
doubt it can be improved I'm not confident it can be made bullet-proof!
Regards,
Peter T
Peter - thanks for working on sample code, but I'm not sure I
understand your solution. I see you are handling the Windows API
GetKeyState in the SheetChange event, but in my experience that event
fires after you have released the key and the KeyState would not
indicate any key as being pressed. Would you mind walking me through
your idea? I'll code it up once I understand it.
thanks!
OK, I've tried to recreate the code I forgot to post last time. As the
previously posted comments/caveats are important I'll re-post them -
[from first post]
Just for ideas, have a go with this in the Worksheet module (later almost
certainly will want to adapt to sheet module events). It's very important to
define the range of changing precedent cells that will trigger a calc,
otherwise the flag will be made a false positive for some future calc event.
In a new wb run the setup routine, then edit/exit-edit A1:A10 in various
ways.
This needs a lot of testing, I've haven't, but already I see problems that
needs sorting, eg:
- If values are pasted, the event order becomes Change > Calc, code as
written wrongly sets the flag
- edit a precedent cell but don't change its value, exit edit mode by
clicking another cell, will set the flag true in advance of a non-existent
calculate event ('cos the change event fired even though the value didn't
change). Could get messy if need to trap previous values to determine 'real'
changes.
- Undo ?
Thinking about it, even if the all above can be fixed I wouldn't be at all
surprised if there are yet more problems to cater for, are you sure you
can't re-think your overall method !
' worksheet module code
Private Declare Function GetKeyState32 Lib "user32" _
Alias "GetKeyState" (ByVal vKey As Integer) As Integer
Dim mbFlag As Boolean
' in a new wb, run SetupTest, edit A1:10, complete entry with Enter
' or leave cell in edit mode with keys like arrow, PageDown etc
' and by clicking anbother cell
Sub SetUpTest()
Names.Add "Precedents", Range("A1:A10")
Range("B1").Formula = "=SUM(A1:A10)"
End Sub
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Debug.Print "Calculate, Change event will follow = " & Not mbFlag
If mbFlag Then Debug.Print
mbFlag = False
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, _
ByVal Target As Range)
Dim bKeyPress As Boolean
Dim k As Long, i As Long
Dim ky(0 To 10) As Long
On Error GoTo errExit
mbFlag = False
If Not Intersect(Target, Range("Precedents")) Is Nothing Then
If Selection.Address <> Target.Address Then
ky(0) = GetKeyState32(vbKeyTab)
ky(1) = GetKeyState32(vbKeyLeft)
ky(2) = GetKeyState32(vbKeyRight)
ky(3) = GetKeyState32(vbKeyUp)
ky(4) = GetKeyState32(vbKeyDown)
ky(5) = GetKeyState32(vbKeyHome)
ky(7) = GetKeyState32(vbKeyEnd)
ky(8) = GetKeyState32(vbKeyPageUp)
ky(9) = GetKeyState32(vbKeyPageDown)
If Application.MoveAfterReturn Then
ky(10) = GetKeyState32(vbKeyReturn)
End If
For i = 0 To 10
If ky(i) < 0 Then
bKeyPress = True
k = i ' indicates the move key in the array if required
Exit For
End If
Next
mbFlag = Not bKeyPress
End If
End If
errExit:
Debug.Print "Change, Calculate will follow = " & mbFlag
If Not mbFlag Then Debug.Print
End Sub
'Undo' is setting the flag the 'wrong' way but I think can work around to
correct, but if code is doing stuff to clear the undo stack that won't be
required.
Regards,
Peter T