Two worksheet changes in one module

LAinAZ

New Member
Joined
Mar 8, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
(DISCLAIMER: I'm a total noob at VBA)

I want to update a "date updated" column automatically whenever I change the contents of one or both of two other columns on a single worksheet. Specifically, if I change the contents of a cell in column A or in column C, I want the "Date Updated" cell in the same row (Column B) to change to today's date. Can I do this?

In researching if it was possible to automatically change the date, I found a post on this site that I modified (because it changed a cell in a different location on the page) and was able to make it work for a single column, but not sure how to do it for two different columns. I created a test worksheet to see if I could make it work. Here's what I have:

Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("A:A")) Is Nothing Then
Target.Offset(0, 1) = Now
End If

End Sub


If I make a change in column A, the date in Column B changes to today's date correctly.
 

Attachments

  • Test Sheet.jpg
    Test Sheet.jpg
    90.8 KB · Views: 4

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Maybe this would work?
VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("A:A")) Is Nothing Then
   Target.Offset(0, 1) = Now
elseif [B]Not Intersect(Target, Range("C:C")) Is Nothing Then
   [B]Target.Offset(0, -1) = Now
else[/B][/B]
End If

End Sub
 
Upvote 0
I have revised your code a bit to take into account the possibility that more than one cell changed at a time, and added a check for column C.

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

Dim Cell As Range

For Each Cell In Target
   If Cell.Column = 1 Or Cell.Column = 3 Then
     Cells(Cell.Row, "B") = Now
   End If
Next Cell

End Sub

Edit: Corrected code
 
Upvote 0
Solution
I have revised your code a bit to take into account the possibility that more than one cell changed at a time, and added a check for column C.

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

Dim Cell As Range

For Each Cell In Target
   If Cell.Column = 1 Or Cell.Column = 3 Then
      Cell.Offset(0, 1) = Now
   End If
Next Cell

End Sub

Edit: Corrected code in red
would this not add NOW to colD if the change was made in colC. Thisnk the op wanted the NOW to still be in ColB

**EDIT - Canx this I see you amended the code as I was typing
 
Upvote 0
I have revised your code a bit to take into account the possibility that more than one cell changed at a time, and added a check for column C.

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

Dim Cell As Range

For Each Cell In Target
   If Cell.Column = 1 Or Cell.Column = 3 Then
     Cells(Cell.Row, "B") = Now
   End If
Next Cell

End Sub

Edit: Corrected code
Sorry, I should have been more specific in my original post - my test worksheet only has 3 columns, but the actual worksheet I'm working with has several columns, A-H. I only want the date in column G to change if there is a change to column D or a change to column H.
 
Upvote 0
All you need to do is change the cell/column references to suit.
 
Upvote 0
Sorry, I should have been more specific in my original post - my test worksheet only has 3 columns, but the actual worksheet I'm working with has several columns, A-H. I only want the date in column G to change if there is a change to column D or a change to column H.
The references to columns are pretty explicit in the code. You just need to update according to your actual layout.
 
Upvote 0
Yep, like I said, total VBA noob, just wasn't sure if it needed to change. I tried it and it's working just fine. Thank you SO much for your help!
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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