Worksheet_Change event

blatham

New Member
Joined
Nov 8, 2005
Messages
47
What is the best way of identifying if the worksheet change event was triggered by anything other than a change in the contents of a cell?

I want to be able to handle cell changes but am not interested in other events such as inserting/deleting rows etc and want to exit the sub in these situations.

The solution presumably involves some sort of test on the 'target' range. Any ideas?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
This is crude but it's a start. If rows or columns have been added or deleted then no action is taken. I don't know what other changes you want to ignore but you might be able to use variations of the time.

If Target.Cells.Count = 256 Or Target.Cells.Count = 65536 Then exit sub
 
Upvote 0
That would enable me to capture insertions/deletions for rows and columns but not for cells.

Any other ideas?
 
Upvote 0
Tell me if the following code will do what you want. It ignores cell additions or deletions. Where I have Msgbox "hello" is where you'd put the code you want to happen if the change is not a cell addition or deletion.
All this code goes in the ThisWorkbook object.


Option Explicit
Dim bStop As Boolean


Private Sub Workbook_Open()
bStop = False
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Static iCount As Integer, rFirst As Range
Dim bEndIt As Boolean

If bStop = True Then
bStop = False
End
End If
iCount = iCount + 1
If iCount = 1 Then
Set rFirst = Target
myundo
End If

If iCount = 2 Then
If Not IsObject(rFirst) Then
bEndIt = True
myredo (bEndIt)
End If
Dim bTest As Boolean
bTest = Target Is rFirst

If (bTest = False) And (rFirst.Address <> Target.Address) Then
bEndIt = True
myredo (bEndIt)
End If

bEndIt = False
myredo (bEndIt)
MsgBox "hello"
End If



End Sub

Sub myundo()
Application.Undo
End Sub

Sub myredo(bEnd As Boolean)
If bEnd = True Then bStop = True
Application.Undo
End Sub
 
Upvote 0
I've modified the code I provided above so that any changes you make to the worksheet as a result of the initial changes will not cause any problems.

I've added a procedure called newcode where you can add the code you want to execute. I hope this is what you had in mind.

Option Explicit
Dim bStop As Boolean


Private Sub Workbook_Open()
bStop = False
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Static iCount As Integer, rFirst As Range
Dim bEndIt As Boolean

If bStop = True Then
bStop = False
End
End If
iCount = iCount + 1
If iCount = 1 Then
Set rFirst = Target
myundo
End If

If iCount = 2 Then
If Not IsObject(rFirst) Then
bEndIt = True
myredo (bEndIt)
End If
Dim bTest As Boolean
bTest = Target Is rFirst

If (bTest = False) And (rFirst.Address <> Target.Address) Then
bEndIt = True
myredo (bEndIt)
End If

bEndIt = False
myredo (bEndIt)
newcode
End If

End Sub

Sub myundo()
Application.Undo
End Sub

Sub myredo(bEnd As Boolean)
If bEnd = True Then bStop = True
Application.Undo
End Sub

Sub newcode()
Application.EnableEvents = False
'place the remainder of your code here
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,207,089
Messages
6,076,518
Members
446,211
Latest member
b306750

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