PC Review


Reply
Thread Tools Rate Thread

How Capture Cell Value Before Change Event Fires?

 
 
=?Utf-8?B?TVZQIFdhbm5hYmU=?=
Guest
Posts: n/a
 
      28th Jun 2007
I am working with the Worksheet change event to perform cell level edits. How
do I get the value of a selection prior to the change event? I assume the
value is stored somewhere in order to enable the undo event to work. I also
read that using the worksheet_Change event disables the undo.

Am I required to write a Worksheet_BeforeChange event, or can I just get the
value so I know what it is?

Thanks in advance
 
Reply With Quote
 
 
 
 
Jim Rech
Guest
Posts: n/a
 
      28th Jun 2007
Wherever the old values is stored they are not accessible to us. So..

Private Sub Worksheet_Change(ByVal Target As Range)
Dim OldValue As Variant
With Application
.EnableEvents = False
.Undo
OldValue = Target.Cells(1).Value
.Undo
.EnableEvents = True
End With
MsgBox "Value was " & OldValue
End Sub

This mades the simplifying assumption only one cell was changed. In 'real
life' you'd need to capture the values in the entire Target range, which can
be more than one cell.

--
Jim
"MVP Wannabe" <MVP (E-Mail Removed)> wrote in message
news:1DF4F9C6-C42A-4560-9A71-(E-Mail Removed)...
|I am working with the Worksheet change event to perform cell level edits.
How
| do I get the value of a selection prior to the change event? I assume the
| value is stored somewhere in order to enable the undo event to work. I
also
| read that using the worksheet_Change event disables the undo.
|
| Am I required to write a Worksheet_BeforeChange event, or can I just get
the
| value so I know what it is?
|
| Thanks in advance


 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      28th Jun 2007
>I am working with the Worksheet change event to perform cell level edits.
>How
> do I get the value of a selection prior to the change event? I assume the
> value is stored somewhere in order to enable the undo event to work. I
> also
> read that using the worksheet_Change event disables the undo.
>
> Am I required to write a Worksheet_BeforeChange event, or can I just get
> the
> value so I know what it is?


There is already a kind of BeforeChange event... it is called
SelectionChange. Add this code to your worksheet's code window...

Dim LastText As String

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
LastText = Target.Value
End Sub

and, when in the Change event, you can reference LastText to see what the
text was when you entered the cell. Note that the Dim statement for the
LastText statement is NOT declared inside any procedures... just place it at
the top of the code window, under your Option Explicit statement if you are
using one (and you should be using one<g>).

Rick

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combo box change event fires twice Smurfette18 Microsoft Excel Programming 4 20th Dec 2007 06:39 PM
Change event on single cell fires many times =?Utf-8?B?WFA=?= Microsoft Excel Programming 3 6th Nov 2006 10:16 PM
Problem with CommandBarComboBox Change Event (Event fires only once) M. Khalid Farooq Microsoft Outlook Interoperability 6 23rd Oct 2006 09:44 AM
Problem with CommandBarComboBox Change Event (Event fires only once) M. Khalid Farooq Microsoft Outlook Program Addins 1 19th Oct 2006 02:34 PM
comboBox TextChanged event fires twice because Items.Insert() fires the event too... Rob Hindman Microsoft Dot NET Framework Forms 2 25th Aug 2003 07:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:54 PM.