Workbook SheetChange

trough

Board Regular
Joined
Oct 26, 2010
Messages
55
I have written plenty of VBA code but never used the event routines, until now. I have a need to perform some actions if certain things happen to certain cells throughout the workbook so I have zeroed in on the Workbook_SheetChange event to cover all sheets. I think I have a decent handle on how to use this except I can't find answers to the following:

1) If a cell clear/delete triggered the event then how do I find what was in the cell before it was cleared?

2) If the cell was moved (i.e. dragged to another cell or a cut/paste occurred) how do I find what new cell the data went to?

Thanks.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
Application.EnableEvents = False

'For first question
Application.Undo
For Each cel In Target
    MsgBox "Previous value : " & cel
Next
Application.Undo

'For second question
MsgBox Target.Address

Application.EnableEvents = True
End Sub
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
Application.EnableEvents = False

'For first question
Application.Undo
For Each cel In Target
    MsgBox "Previous value : " & cel
Next
Application.Undo

'For second question
MsgBox Target.Address

Application.EnableEvents = True
End Sub
Thanks.
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
Application.EnableEvents = False

'For first question
Application.Undo
For Each cel In Target
    MsgBox "Previous value : " & cel
Next
Application.Undo

'For second question
MsgBox Target.Address

Application.EnableEvents = True
End Sub
I thought this was what I was looking for except I found it returns you to the cell that was changed and doesn't advance to the next cell upon hitting <enter> as Excel normally would so it kind of messes up the normal functionality of working in Excel.
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
Application.EnableEvents = False

'For first question
Application.Undo
For Each cel In Target
    MsgBox "Previous value : " & cel
Next
Application.Undo
If Target.Count = 1 Then ActiveCell(1, 2).Select 'move right
'If Target.Count = 1 Then ActiveCell(1, 0).Select 'move left
'If Target.Count = 1 Then ActiveCell(0, 1).Select 'move up
'If Target.Count = 1 Then ActiveCell(2, 1).Select 'movedown

'For second question
MsgBox Target.Address

Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,093
Latest member
ripvw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top