Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Order of Event Firing with Automatic Calculation

We noticed that if Automatic Calculation is enabled and data is
entered into a cell followed by the "Enter" key, the sequence is that
the SheetCalculate event is fired first and then the
SheetChange event. Wheres if the user clicks a different
cell after entering the data (instead of hitting "Enter") the order is
reversed with the SheetCalc event only firing after the
SheetChange event.
We need to handle the input to write back to some storage, but when
the user is hitting "Enter" the data is retrieved from the storage
first since the CalcHandler fires first and hence we lose the value
the user entered in that cell. Is there some way inside the
SheetCalculate handler for us to differentiate whether there is a
SheetChange event that is about to be called? Is there some other
suggestion as to how we can handle this case?
thanks!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Order of Event Firing with Automatic Calculation

Hmm, not straightforward!

First thought was to compare target & selection addresses, if not same
suggests the user clicked another cell whilst in edit mode, with the
implication the event order is Change Calc.

But respective addresses are also not same if user presses say an arrow key
(& various others) whilst in edit mode. But the event order is the same as
Enter key, ie Calc Change, ie your "SheetChange event is about to be
called" after Calc.

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 !

Regards,
Peter T


wrote in message
ups.com...
We noticed that if Automatic Calculation is enabled and data is
entered into a cell followed by the "Enter" key, the sequence is that
the SheetCalculate event is fired first and then the
SheetChange event. Wheres if the user clicks a different
cell after entering the data (instead of hitting "Enter") the order is
reversed with the SheetCalc event only firing after the
SheetChange event.
We need to handle the input to write back to some storage, but when
the user is hitting "Enter" the data is retrieved from the storage
first since the CalcHandler fires first and hence we lose the value
the user entered in that cell. Is there some way inside the
SheetCalculate handler for us to differentiate whether there is a
SheetChange event that is about to be called? Is there some other
suggestion as to how we can handle this case?
thanks!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Order of Event Firing with Automatic Calculation

Peter - first off, thanks for acknowledging and attempting to solve
this challenging Excel issue. If we can just solve it so it handles
the Enter key and the case wher eyou click of the sale after data
entry, I'll worry about any other cases. But I can't even solve
those!
The idea of comparing Target and selection doesn't work because the
value of Target is already changed when the SheetChange event gets
fired and the new value entered is lost.
We have to keep the behavior since that is how it used to work when we
didn't support AutoCalc and now that we are going to support AutoCalc
and ManualCalc, we have to handle this case.
If anyone else is up to the challenge please help!
thanks.

On Oct 9, 7:44 pm, "Peter T" <peter_t@discussions wrote:
Hmm, not straightforward!

First thought was to compare target & selection addresses, if not same
suggests the user clicked another cell whilst in edit mode, with the
implication the event order is Change Calc.

But respective addresses are also not same if user presses say an arrow key
(& various others) whilst in edit mode. But the event order is the same as
Enter key, ie Calc Change, ie your "SheetChange event is about to be
called" after Calc.

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 !

Regards,
Peter T

wrote in message

ups.com...

We noticed that if Automatic Calculation is enabled and data is
entered into a cell followed by the "Enter" key, the sequence is that
the SheetCalculate event is fired first and then the
SheetChange event. Wheres if the user clicks a different
cell after entering the data (instead of hitting "Enter") the order is
reversed with the SheetCalc event only firing after the
SheetChange event.
We need to handle the input to write back to some storage, but when
the user is hitting "Enter" the data is retrieved from the storage
first since the CalcHandler fires first and hence we lose the value
the user entered in that cell. Is there some way inside the
SheetCalculate handler for us to differentiate whether there is a
SheetChange event that is about to be called? Is there some other
suggestion as to how we can handle this case?
thanks!



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Order of Event Firing with Automatic Calculation

How strange, seems I forgot to post the event routines in my previous post!
Annoyingly I didn't keep a copy of the code, will try and recreate later.

The idea of comparing Target and selection doesn't work because the
value of Target is already changed


I had in mind to compare Target & selection addresses, which may or may not
be the same depending on how the edit was completed.

Whilst it the code I worked out previously went a long way to establishing
what you wanted to know, it was far from foolproof.

Regards,
Peter T

wrote in message
ups.com...
Peter - first off, thanks for acknowledging and attempting to solve
this challenging Excel issue. If we can just solve it so it handles
the Enter key and the case wher eyou click of the sale after data
entry, I'll worry about any other cases. But I can't even solve
those!
The idea of comparing Target and selection doesn't work because the
value of Target is already changed when the SheetChange event gets
fired and the new value entered is lost.
We have to keep the behavior since that is how it used to work when we
didn't support AutoCalc and now that we are going to support AutoCalc
and ManualCalc, we have to handle this case.
If anyone else is up to the challenge please help!
thanks.

On Oct 9, 7:44 pm, "Peter T" <peter_t@discussions wrote:
Hmm, not straightforward!

First thought was to compare target & selection addresses, if not same
suggests the user clicked another cell whilst in edit mode, with the
implication the event order is Change Calc.

But respective addresses are also not same if user presses say an arrow

key
(& various others) whilst in edit mode. But the event order is the same

as
Enter key, ie Calc Change, ie your "SheetChange event is about to be
called" after Calc.

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 !

Regards,
Peter T

wrote in message

ups.com...

We noticed that if Automatic Calculation is enabled and data is
entered into a cell followed by the "Enter" key, the sequence is that
the SheetCalculate event is fired first and then the
SheetChange event. Wheres if the user clicks a different
cell after entering the data (instead of hitting "Enter") the order is
reversed with the SheetCalc event only firing after the
SheetChange event.
We need to handle the input to write back to some storage, but when
the user is hitting "Enter" the data is retrieved from the storage
first since the CalcHandler fires first and hence we lose the value
the user entered in that cell. Is there some way inside the
SheetCalculate handler for us to differentiate whether there is a
SheetChange event that is about to be called? Is there some other
suggestion as to how we can handle this case?
thanks!





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Order of Event Firing with Automatic Calculation

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




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Order of Event Firing with Automatic Calculation

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!


On Oct 11, 4:30 pm, "Peter T" <peter_t@discussions wrote:
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



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Order of Event Firing with Automatic Calculation

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





wrote in message
oups.com...
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!


On Oct 11, 4:30 pm, "Peter T" <peter_t@discussions wrote:
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





  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Order of Event Firing with Automatic Calculation

Peter - first off, thanks for acknowledging and attempting to solve
this challenging Excel issue. If we can just solve it so it handles
the Enter key and the case where you click off the cell after data
entry, I'll worry about any other cases. But I can't even solve
those!
The idea of comparing Target and Selection doesn't work because the
value of Target is already changed when the SheetChange event gets
fired and the new value entered is lost.
We have to keep the behavior since that is how it used to work when we
didn't support AutoCalc and now that we are going to support AutoCalc
and ManualCalc, we have to handle this case.
If anyone else is up to the challenge please help!
thanks.


On Oct 9, 7:44 pm, "Peter T" <peter_t@discussions wrote:
Hmm, not straightforward!

First thought was to compare target & selection addresses, if not same
suggests the user clicked another cell whilst in edit mode, with the
implication the event order is Change Calc.

But respective addresses are also not same if user presses say an arrow key
(& various others) whilst in edit mode. But the event order is the same as
Enter key, ie Calc Change, ie your "SheetChange event is about to be
called" after Calc.

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 !

Regards,
Peter T

wrote in message

ups.com...

We noticed that if Automatic Calculation is enabled and data is
entered into a cell followed by the "Enter" key, the sequence is that
the SheetCalculate event is fired first and then the
SheetChange event. Wheres if the user clicks a different
cell after entering the data (instead of hitting "Enter") the order is
reversed with the SheetCalc event only firing after the
SheetChange event.
We need to handle the input to write back to some storage, but when
the user is hitting "Enter" the data is retrieved from the storage
first since the CalcHandler fires first and hence we lose the value
the user entered in that cell. Is there some way inside the
SheetCalculate handler for us to differentiate whether there is a
SheetChange event that is about to be called? Is there some other
suggestion as to how we can handle this case?
thanks!






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Event Firing with Automatic Calculation [email protected] Excel Programming 1 October 10th 07 07:34 AM
Click event on menu item is lost after first time firing of the event [email protected] Excel Programming 1 April 2nd 07 01:25 PM
Change Event not firing L. Howard Kittle Excel Programming 3 December 16th 06 05:38 AM
Event sometimes stops firing? HotRod Excel Programming 7 May 5th 05 12:20 AM
OnTime event not firing in Workbook_Open event procedure GingerTommy Excel Programming 0 September 24th 03 03:18 PM


All times are GMT +1. The time now is 06:59 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"