I'm trying to get code to work for a range of cells but so far I've had no luck getting it to work. I have code that works for a single cell but can't get anything to work for the range.
I have dates in column C, rows 5-14. What I'm trying to accomplish is that anytime the date in any one of those cells changes, the original date is copied and then placed inside the cell directly to the right, in column D. So if cell C5 is 12/1/23 and I change the date to 12/5/23, the original date is copied prior to the change and then places that date (12/1/23) into cell D5.
Here is the code that I have working when applied to just cells C5 and D5:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("C5").Address Then
Application.EnableEvents = False
Dim sOldValue As String, sNewValue As String
sNewValue = Target.Value
Application.Undo
sOldValue = Range("C5").Value
Target.Value = sNewValue
Worksheets("Sheet1").Range("D5") = sOldValue
Application.EnableEvents = True
End If
End Sub
This code does exactly what I'm looking for but need to get it to where it works for all of the cells in column C when any one of them are changed. I've tried modifying the code above to ("C5:C14") and ("D5:D14") with no luck and also tried this code below, also with no luck.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("C5:C14").Address Then
Application.EnableEvents = False
Dim sOldValue As String, sNewValue As String
sNewValue = Target.Value
Application.Undo
Dim rOld As Range
Set rOld = Range("C5:C14").Value
Target.Value = sNewValue
Range("D5:D14").Value = rOld.Value
Application.EnableEvents = True
End If
End Sub
I've searched all over and have found some similar topics, but nothing has really fit for what I'm attempting to do. Any help is greatly appreciated.