ioecho
asked on
Capturing Date and Time Stamp
Hi I found this code online. So far this code works fine. When I enter a value in column A, the date is recorded in the adjacent column. Cool! But, I do not understand this code and would like to add comments to each line so I understand how it works. Could someone clearly dissect this code for me? Also, if I were to make the macro more robust by capturing the date and time when a particular target value is entered into column A ,.. for example [If there is a drop down list of restricted values "Yes" and "No"], I would want to capture the date and time when the user chooses "yes", and only the first instance of when this event occurs. how would that code look? Please include comments. Much Appreciated.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A2:A100")) Is Nothing Then
With Target(1, 2)
.Value = Date
.EntireColumn.AutoFit
End With
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A2:A100")) Is Nothing Then
With Target(1, 2)
.Value = Date
.EntireColumn.AutoFit
End With
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Regarding your desired changes, it works like this: You check if the B column already has a value. Only if not, then check if the current cell's value is "yes", and if so set the B value to the current date.
' * Private Sub = A procedure which cannot be seen outside of the modul part it is in
' * Worksheet_Change is the default event handler called if a cell of this worksheet changes
' * Target is a parameter, and ByVal means the Sub cannot send changes to it back to the caller
Private Sub Worksheet_Change(ByVal Target As Range)
' * more than one cell changed => leave sub, do nothing
If Target.Cells.Count > 1 Then Exit Sub
' * If the target cell is NOT in the range of cells A2 to A100 (column A, rows 2 to 100) then leave sub
If Intersect(Target, Range("A2:A100")) Is Nothing Then Exit Sub
' * If the B cell contains a value, leave sub
If Target(1,2) <> 0 Then Exit Sub
' * if current cell contains "yes", set B cell to current date, and fit B column to values
If Target.Value = "yes" then
Target(1,2).Value = Date
Target(1,2).EntireColumn.AutoFit
End If
End Sub
I want to correct something in my previous post.
The comments for this code that I wrote were wrong:
Please ignore this sentence from my comments:
'if the target cell and a value in the range of cells of column A (from row 2 through 100) is the same, the following will be executed.'
I think Qlemo has a better response on this one.
The comments for this code that I wrote were wrong:
If Not Intersect(Target, Range("A2:A100")) Is Nothing Then
Please ignore this sentence from my comments:
'if the target cell and a value in the range of cells of column A (from row 2 through 100) is the same, the following will be executed.'
I think Qlemo has a better response on this one.
well explained Qlemo
Open in new window
For more about the With clause, see this link: http://www.homeandlearn.org/with_end_with.html