Can you please write me a vba code that will do the following:
When I make a change in column I, J or K of my sheet,
write a time stamp to column L for the row where the change was made.
Assistant
Certainly! You can achieve this by using a VBA event handler that responds to changes made in specific columns (I, J, and K) of your worksheet. Below is the VBA code that will write a timestamp in column L whenever you make a change in any of the cells in columns I, J, or K.
Open your Excel workbook.
Press ALT + F11 to open the VBA editor.
User
This code is not working: Dim TimeRangeK As Range
Dim cellK As Range
' Define the range for columns K
Set TimeRangeK = Intersect(Target, Me.Range("K"))
code
' Check if the change occurred in columns K
If Not TimeRangeK Is Nothing Then
Me.Unprotect Password:="edit"
Application.EnableEvents = False ' Prevent infinite loops
For Each cellK In TimeRangeK
' Write the current timestamp to column L of the changed row
Me.Cells(cellK.Row, "L").Value = Now
Next cellK
Application.EnableEvents = True ' Re-enable events
Me.Protect Password:="edit"
End If
Assistant
In the Project Explorer window, find your workbook, and then the worksheet where you want to add this functionality (for example, Sheet1).
Double-click on the sheet (like Sheet1) to open its code window.
Copy and paste the following code into the code window:
vba
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Dim cell As Range
' Define the range for columns I, J, K
Set AffectedRange = Intersect(Target, Me.Range("I:K"))
' Check if the change occurred in columns I, J, or K
If Not AffectedRange Is Nothing Then
Application.EnableEvents = False ' Prevent infinite loops
For Each cell In AffectedRange
' Write the current timestamp to column L of the changed row
Me.Cells(cell.Row, "L").Value = Now
Next cell
Application.EnableEvents = True ' Re-enable events
End If
End Sub
Explanation of the Code:
Worksheet_Change: This is an event that fires whenever a change is made in the worksheet.
Target: This is the range of cells that were changed.
Intersect: This function checks if the changed cells intersect with the specified columns (I, J, K).
Application.EnableEvents: Used to prevent the event from triggering again while updating the timestamp, which could lead to an infinite loop.
Important Notes:
Make sure to save your work before testing the code, as VBA can't always be undone with the usual CTRL + Z.
After placing the code, go back to your Excel sheet and try making a change in columns I, J, or K. You should see a timestamp appear in column L of the same row.
This should successfully log the current time whenever a change is made in either of those columns.
It looks like you're trying to modify the code to check specifically for changes in column K and also include unprotecting and protecting the worksheet within the code. This is a good approach, but there are a couple of adjustments and clarifications needed to ensure it works properly.
Complete Code Example
Here's an updated version of the entire Worksheet_Change event to handle changes in columns I, J, and K while unprotecting and protecting the sheet:
vba
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Dim cell As Range
' Define the range for columns I, J, and K
Set AffectedRange = Intersect(Target, Me.Range("I:K"))
' Check if the change occurred in columns I, J or K
If Not AffectedRange Is Nothing Then
Me.Unprotect Password:="edit" ' Unprotect the sheet
Application.EnableEvents = False ' Prevent infinite loops
For Each cell In AffectedRange
' Write the current timestamp to column L of the changed row
Me.Cells(cell.Row, "L").Value = Now
Next cell
Application.EnableEvents = True ' Re-enable events
Me.Protect Password:="edit" ' Protect the sheet again
End If
End Sub
Explanation of the Code:
Dim AffectedRange As Range: This variable will store the range of cells in columns I, J, and K that were changed.
Intersect(Target, Me.Range("I:K")): This checks if the modified cells are within the specified range (columns I, J, or K).
Unprotect/Protect: The sheet is unprotected before making changes, and then protected again afterwards.
Application.EnableEvents = False: This prevents the Worksheet_Change event from being triggered again when you update the timestamps, avoiding an infinite loop.
Renting the timestamp: Me.Cells(cell.Row, "L").Value = Now writes the current date and time to column L in the same row.
Important Points:
Worksheet Protection: Ensure that the password ("edit" in this case) is correct, and that protecting/unprotecting the sheet is necessary for your use case.
Placement: Make sure that this code is placed in the code window for the specific worksheet you want it to operate on (e.g., Sheet1 or Sheet2).
Testing: After inserting the code, test it by making changes to columns I, J, or K and check if the timestamp appears in column L.
By using the above code, you should now see proper timestamps logged in column L when changes are made in any of the designated columns, provided that the sheet's protection is appropriately handled.