What is the best way to incorporate more than one Private Sub Worksheet Change in one sheet?
Is there a work around using modules?
Here is the code, I can't have all 3 work at the same time. They all do different things.
I might even add more Private Sub Worksheet Change and I am trying to find the easiest way to run them.
Code
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim ws As Worksheet
Dim str As String
Dim i As Integer
Dim rngDV As Range
Dim rng As Range
If Target.Count > 1 Then Exit Sub
Set ws = Worksheets("DD")
If Target.Row > 1 Then
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo 0
If rngDV Is Nothing Then Exit Sub
If Intersect(Target, rngDV) Is Nothing Then Exit Sub
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
On Error Resume Next
Set rng = ws.Range(str)
On Error GoTo 0
If rng Is Nothing Then Exit Sub
If Application.WorksheetFunction _
.CountIf(rng, Target.Value) Then
Exit Sub
Else
i = ws.Cells(Rows.Count, rng.Column).End(xlUp).Row + 1
ws.Cells(i, rng.Column).Value = Target.Value
rng.Sort Key1:=ws.Cells(1, rng.Column), _
Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End If
End Sub
'https://www.mrexcel.com/forum/excel-questions/457000-macro-prevent-duplicate-entries.html
Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If .Column <> 2 Or .Cells.Count > 1 Then Exit Sub
If WorksheetFunction.CountIf(Columns(.Column), .Value) > 1 Then
Application.DisplayAlerts = False
.ClearContents
Application.DisplayAlerts = True
MsgBox "This PGRB code has previously been used. You can't use the same code twice."
End If
End With
End Sub
'https://www.mrexcel.com/forum/excel-questions/918896-show-last-date-modified-every-row.html
Private Sub Worksheet_Change(ByVal Target As Range)
Define variable
Dim Cell As Range
' If you update any cell in column O (update as required) then...
If Not Intersect(Target, Range("B:B")) Is Nothing Then
' If only one cell is updated then...
If Target.Cells.Count = 1 Then
' Timestamp column P of the target row
Range("C" & Target.Row).Value = Now()
' Else if more than one cell is updated at once then...
Else
' For each cell in the selection
For Each Cell In Selection
' If the updated value is not blank
If Cell.Value <> "" Then
' Timestamp column P of the target row
Range("C" & Cell.Row).Value = Now()
End If
' Check next cell in selection
Next Cell
End If
End If
End Sub
Display More