Trying to apply 2 Worksheet Changes

MLC1980

New Member
Joined
Oct 1, 2024
Messages
13
Office Version
  1. 2016
Platform
  1. Windows
Hi, this is my first port on the forum and I admit everything I know about VBA is self taught. I currently have a spreadsheet with a 'Worksheet Change' code to allow multiple dropdown selections in columns L and M. That code is working perfectly.

My issue is that I now want to add a column to the spreadsheet where a macro automatically adds the details of when the corresponding row was 'Last Updated' (date updated and who by). I found a code which looks like it should work but it is also a 'Worksheet Change' and I don't know how to combine the two, can anyone help?

The code that I currently have for the multiple dropdown selections is:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Not Intersect(Target, Range("L2:M5000")) Is Nothing Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & vbNewLine & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub

The code that I found for the 'Last Updated' column is as follows (although I do also want it to include the application.username if that's possible):
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("$A:$V"), Target) Is Nothing Then
Range("W" & Target.Row).Value = Date
End If
End Sub

Thanks
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Welcome to the Board!

You cannot multiple Worksheet_Change procedures in the same module, so you need to combine all your code under one Worksheet_Change procedure (just have different blocks of code). The key is, you do NOT want anything in the first block of code that will exit the code before getting to the second block.

I tried updating and re-arranging your code. See if this does what you want:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Oldvalue As String
    Dim Newvalue As String

    On Error GoTo Exitsub

'***BLOCK 1***
If Not Application.Intersect(Range("$A:$V"), Target) Is Nothing Then
    Application.EnableEvents = False
    Range("W" & Target.Row).Value = Date
End If

'***BLOCK 2***
    If Not Intersect(Target, Range("L2:M5000")) Is Nothing Then
        If Not Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            If Target.Value <> "" Then
                Application.EnableEvents = False
                Newvalue = Target.Value
                Application.Undo
                Oldvalue = Target.Value
                If Oldvalue = "" Then
                    Target.Value = Newvalue
                Else
                    If InStr(1, Oldvalue, Newvalue) = 0 Then
                        Target.Value = Oldvalue & vbNewLine & Newvalue
                    Else
                        Target.Value = Oldvalue
                    End If
                End If
            End If
        End If
    End If


Exitsub:
    Application.EnableEvents = True
    
End Sub
Also note: When posting your code, please use the Code tags, like I did. It maintains spacing and adds colors, making your code much easier to read.
See here for details on how to do that (it is very easy): How to Post Your VBA Code
 
Upvote 0
Solution

Forum statistics

Threads
1,223,868
Messages
6,175,084
Members
452,611
Latest member
bls2024

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