Automatic date in a cell, when the corresponding row is updated or changed

Sarahd1

New Member
Joined
Mar 15, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi everyone. I have been trying to figure out a VBA code that will automatically input the date which the corresponding row is updated. I need the code in column P, starting in row 4, and I already have code in the document for some other columns. The goal here is to be able to see the last date that each row was updated.
Can anyone please advise a code that will do this? The rows are a mixture of free text and some dependent drop down lists. I will post the code and a screenshot of the code, that I already have below, can you please advise where to put the additional code for the dates? I have never used two different codes in one document before. Thank you!! :)
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Update by Extendoffice 2018/06/04

    Application.EnableEvents = False

    If Target.Column = 2 And Target.Validation.Type = 3 Then
        Target.Offset(0, 1).Value = ""
    End If

    If Target.Column = 4 And Target.Validation.Type = 3 Then
        Target.Offset(0, 1).Value = ""
    End If

    If Target.Column = 5 And Target.Validation.Type = 3 Then
        Target.Offset(0, 1).Value = ""
    End If

    If Target.Column = 6 And Target.Validation.Type = 3 Then
        Target.Offset(0, 1).Value = ""
    End If

    Application.EnableEvents = True
    
End Sub
 

Attachments

  • Code in worksheet (sheet 1).PNG
    Code in worksheet (sheet 1).PNG
    198.4 KB · Views: 22
And do you want it to work only if the cell changed has a Data Validation applicable to it, or any cell in the range A:O?
Any cell in the range A:O. They are a mix of data validation and free text
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Any cell in the range A:O. They are a mix of data validation and free text
Just to confirm - when I say for any cell, I am only referring to the portion of code for the dates.
I need the date to update if any cell in the range is changed or a new row of data is added.
But the previous code for the data validation drop downs (columns, 2, 4, 5, 6), I need that to remain as it was before. Not for all cells in the range A:O.
 
Upvote 0
OK, give this a try:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge = 1 And Not Intersect(Range("A:O"), Target) Is Nothing Then
    Application.EnableEvents = False
        If Target.Validation.Type = 3 Then
            Target.Offset(, 1) = ""
            Range("P" & Target.Row).Value = Date
        End If
        Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
OK, give this a try:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge = 1 And Not Intersect(Range("A:O"), Target) Is Nothing Then
    Application.EnableEvents = False
        If Target.Validation.Type = 3 Then
            Target.Offset(, 1) = ""
            Range("P" & Target.Row).Value = Date
        End If
        Application.EnableEvents = True
    End If
End Sub
Thanks Kevin
The code before this one was more close to what I needed, as it affected the correct drop downs. The only change I needed was that if any cell in the row was updated or changed (starting in row 4), the date would update in column P.

I had two issues with this code -
Firstly, this is now affecting the drop downs between C and D. C and D are not dependent lists, and now if something is already selected in column D, and column C is changed, then D blanks. These columns aren't connected and I don't need changes in column C to affect column D.

Secondly, After I used the code, I typed in one of the free text cells to test whether the date would work, and I was given error code 1004. I clicked debug and it highlighted the error row as (bold below). I deleted the code and replaced it with the previous code, but now that won't work either
OK, give this a try:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge = 1 And Not Intersect(Range("A:O"), Target) Is Nothing Then
    Application.EnableEvents = False
[B]        If Target.Validation.Type = 3 Then[/B]
            Target.Offset(, 1) = ""
            Range("P" & Target.Row).Value = Date
        End If
        Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
Thanks Kevin
The code before this one was more close to what I needed, as it affected the correct drop downs. The only change I needed was that if any cell in the row was updated or changed (starting in row 4), the date would update in column P.

I had two issues with this code -
Firstly, this is now affecting the drop downs between C and D. C and D are not dependent lists, and now if something is already selected in column D, and column C is changed, then D blanks. These columns aren't connected and I don't need changes in column C to affect column D.

Secondly, After I used the code, I typed in one of the free text cells to test whether the date would work, and I was given error code 1004. I clicked debug and it highlighted the error row as (bold below). I deleted the code and replaced it with the previous code, but now that won't work either
OK, try the following. Replace all existing code with this code, save the file then test.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge = 1 And Not Intersect(Range("A:O"), Target) Is Nothing Then
    Application.EnableEvents = False
        Range("P" & Target.Row).Value = Date
        Select Case Target.Column
            Case Is = 2, 4, 5, 6
                If Target.Validation.Type = 3 Then
                    Target.Offset(, 1) = ""
                End If
        End Select
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
OK, try the following. Replace all existing code with this code, save the file then test.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge = 1 And Not Intersect(Range("A:O"), Target) Is Nothing Then
    Application.EnableEvents = False
        Range("P" & Target.Row).Value = Date
        Select Case Target.Column
            Case Is = 2, 4, 5, 6
                If Target.Validation.Type = 3 Then
                    Target.Offset(, 1) = ""
                End If
        End Select
    End If
    Application.EnableEvents = True
End Sub
Thanks Kevin! that one seemed to do the trick :)
However, I am now getting an error again, and I don't know why. I will attach screenshots. Note, I amended the code to remove column 4 as I made
1004 Error 2203.PNG
Debug error 2203.PNG
some changed to the document and no longer needed that column. You can see in the sceenshots the error I am getting, and where the error is showing up. I clicked on 'debug' from the error window, and that row is highlighted. Any thoughts? Is this a result of the code or something else I have done?
 
Upvote 0
What did you do exactly to get the above error? Did you make a change in a column, if so which one, and what change did you make?
 
Upvote 0
What did you do exactly to get the above error? Did you make a change in a column, if so which one, and what change did you make?
I can't remember exactly, but I think it was when I was typing free text in one of the columns or selecting the drop downs. it has happened with previous codes, definitely when I entered free text in a cell or used the drop downs. I was having this issue previously with previous codes as well.
I didn't change any of the formatting of the columns (i.e change where drop downs appeared or what cells were free text. It was simply using the document as it will be used (it is a template so data will be entered to the columns).
 
Upvote 0
OK. Unfortunately, I can't replicate the problem you're having on the sheet I created to test the code - but then again, I'm only guessing the actual layout etc. Could you please provide a copy of the actual sheet using the XL2BB Tool, or alternatively, share your file via a file-sharing website such as Dropbox or something similar?
 
Upvote 0

Forum statistics

Threads
1,223,919
Messages
6,175,371
Members
452,638
Latest member
Oluwabukunmi

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