worksheet change event to other sheets

tomanton

New Member
Joined
Jan 28, 2014
Messages
26
hi all

I've got multiple worksheets in one s/s and have created a worksheet change event in one and cant find a quick way of applying the code to the sheets?

is it actually possible? or a simple paste/copy?

Thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Do you want to use the same code for the change event of all the sheets in the workbook?

If you do take a look at the workbook level event SheetChange.
 
Upvote 0
yeah you're right, seems sheetchange to be what I need, but I've received out of stack space error for this
Code:
Private Sub workbook_sheetchange(ByVal Sh As Object, ByVal target As Range)
    Dim clickRng As Range
    Dim lastRow As Long
    
    lastRow = Range("e3").End(xlDown).Row
    Set clickRng = Range("a3:d" & lastRow)
    
    If Not Intersect(target, clickRng) Is Nothing And Cells(2, 1).Value = "AD&S Team" Then
        If ActiveCell.Value = 1 Or ActiveCell.Value = 2 Or ActiveCell.Value = 3 Or ActiveCell.Value = 4 Then
        ActiveCell.Value = 0
    Else
        Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, 4)).Value = 0
        If ActiveCell.Column = 1 Then
            ActiveCell.Value = 1
        ElseIf ActiveCell.Column = 2 Then
            ActiveCell.Value = 2
        ElseIf ActiveCell.Column = 3 Then
            ActiveCell.Value = 3
        Else
            ActiveCell.Value = 4
        End If
    End If
    End If
End Sub

as I only changed the top bit from worksheetchange
 
Upvote 0
Your code is making changes so it's going to call itself.

To prevent that add this at the start of the code,
Code:
Application.EnableEvents = False
and this at the end.
Code:
Application.EnableEvents = True
By the way instead of using ActiveCell you should use Target as that's a reference to the cell(s) that has been changed.

Similarly you might want to use Sh which is a reference to the sheet the change has been made on.
 
Upvote 0
TThat's brilliant all is working, thanks. I also would like to eliminate the error when two cells are selected. Any ideas?
 
Upvote 0
I've added a piece of code to check the number of cells selected and stop the sub in case more then 1 is selected but exit sub doesn't work and not sure how to get the code work or recover after selecting two or more cells:
Code:
Private Sub workbook_sheetselectionchange(ByVal Sh As Object, ByVal target As Range)
Application.EnableEvents = False
    Dim clickRng As Range
    Dim lastRow As Long
    
    lastRow = Range("e3").End(xlDown).Row
    Set clickRng = Range("a3:d" & lastRow)
  
    If InStr(target.Address, ":") > 0 Then
    Exit Sub
    End If
    
    If Not Intersect(target, clickRng) Is Nothing And Cells(2, 1).Value = "AD&S Team" Then
        If target.Value = 1 Or target.Value = 2 Or target.Value = 3 Or target.Value = 4 Then
        target.Value = 0
    Else
        Range(Cells(target.Row, 1), Cells(target.Row, 4)).Value = 0
        If target.Column = 1 Then
            target.Value = 1
        ElseIf target.Column = 2 Then
            target.Value = 2
        ElseIf target.Column = 3 Then
            target.Value = 3
        Else
            target.Value = 4
        End If
    End If
    End If
    
Application.EnableEvents = True
End Sub
 
Upvote 0
I have sorted it out myself just by researching, changed the position of Application.EnableEvents = False and it worked
 
Upvote 0

Forum statistics

Threads
1,221,441
Messages
6,159,904
Members
451,601
Latest member
terrynelson55

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