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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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,224,602
Messages
6,179,839
Members
452,948
Latest member
UsmanAli786

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