Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Trapping value before and after change

I am using the Worksheet_Change event to trigger a macro saving the new cell
value to a VBA variable. I use the Worksheet_SelectionChange event to capture
the old value before the change occurs. How can I trigger a macro when the
cell being changed is already selected? In other words, is there a way to
capture the old value as soon as the editing begins, then capture the new
value when editing ends?

Goody
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Trapping value before and after change

The worksheet_Selection change event is only triggered if you actually make a
change in selection. If you enter something in Call A2 and then re-enter
something in the same cell, it won't be triggered the 2nd time. The only
way I can see around this is to capture the value at the end of the worksheet
change event. If you do a selection change, the value will be replaced then.
--
HTH,

Barb Reinhardt



"Goody" wrote:

I am using the Worksheet_Change event to trigger a macro saving the new cell
value to a VBA variable. I use the Worksheet_SelectionChange event to capture
the old value before the change occurs. How can I trigger a macro when the
cell being changed is already selected? In other words, is there a way to
capture the old value as soon as the editing begins, then capture the new
value when editing ends?

Goody

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Trapping value before and after change

Hi Goody,

The following code example gets the previous entry by using Undo. However,
note that if the user makes an error in entering data and has to re-enter
then it does not get the original data; only the data there immediately prior
to the last entry. Running the code also cancels the undo's in the
interactive mode.

The code is really only suitable where you want to use code to validate an
entry and probably put it back to original value if new value is not valid.
See my second example. (Note normal validation would normally be used in lieu
of the code example and the code is only a simplistic example.)

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo Re_EnableEvents
Application.EnableEvents = False

Dim newValue As Variant
Dim prevValue As Variant

newValue = Target.Value
Application.Undo

prevValue = Target.Value
Target.Value = newValue

MsgBox "Previous value of " _
& Target.Address & " was: " _
& prevValue

Re_EnableEvents:
Application.EnableEvents = True
End Sub


Entry validation.
Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo Re_EnableEvents
Application.EnableEvents = False

Dim newValue As Variant
Dim prevValue As Variant

newValue = Target.Value
Application.Undo

prevValue = Target.Value

If newValue <= prevValue Then
MsgBox "The value you entered is invalid." _
& vbLf & "Please re-enter."
Else
Target.Value = newValue
End If

Re_EnableEvents:
Application.EnableEvents = True
End Sub


--
Regards,

OssieMac


"Goody" wrote:

I am using the Worksheet_Change event to trigger a macro saving the new cell
value to a VBA variable. I use the Worksheet_SelectionChange event to capture
the old value before the change occurs. How can I trigger a macro when the
cell being changed is already selected? In other words, is there a way to
capture the old value as soon as the editing begins, then capture the new
value when editing ends?

Goody

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Trapping value before and after change

I found a workaround. When the worksheet is activated, I stored the values of
all cells in the area most likely to contain any modificationsin an array.
Thus, even if the selection is not changed, the value is stored. Then, when a
cell is changed, I get the previous value from the array.

Thanks for your help.

"Goody" wrote:

I am using the Worksheet_Change event to trigger a macro saving the new cell
value to a VBA variable. I use the Worksheet_SelectionChange event to capture
the old value before the change occurs. How can I trigger a macro when the
cell being changed is already selected? In other words, is there a way to
capture the old value as soon as the editing begins, then capture the new
value when editing ends?

Goody

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
Error trapping hshayh0rn Excel Programming 1 May 4th 06 05:42 PM
error trapping JohnE Excel Programming 2 April 27th 06 03:50 PM
error trapping [email protected] Excel Programming 2 April 5th 05 12:52 AM
error trapping [email protected] Excel Programming 2 January 20th 05 10:07 PM
Error Trapping Options Change in Excel 1997 Frank DeLuccia Excel Programming 0 April 28th 04 11:08 PM


All times are GMT +1. The time now is 06:50 PM.

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"