The code I have below records the last three entries with a comment box with the users name, date, and that they did. The question I have is how do I get this to record only if data is entered? Right now if I hit a back space in the cell or clear contents it will record that. And I'd prefer not to have it record it.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim CellComment As Comment
Dim CommentText As String
Dim aryVals As Variant
Dim i As Long
If Target.Count = 1 Then
Set CellComment = Target.Comment
If Not CellComment Is Nothing Then
If InStr(1, CellComment.Text, "User: ") > 0 Then
If InStr(1, Mid(CellComment.Text, InStr(1, CellComment.Text, ":") + 1), ",") Then
aryVals = Split(Mid(CellComment.Text, InStr(1, CellComment.Text, ":") + 1), ",")
Select Case Len(CellComment.Text) - Len(Replace(CellComment.Text, ",", vbNullString))
Case 1
aryVals = Array(Environ("Username ") & Chr(32) & "entered " & Chr(32) & Target.Value & Chr(32) & "on " & Format(Now(), "mm/dd/yy"), _
aryVals(0), _
aryVals(1))
Case 2
For i = UBound(aryVals) To LBound(aryVals) + 1 Step -1
aryVals(i) = aryVals(i - 1)
Next
aryVals(LBound(aryVals)) = Environ("Username ") & Chr(32) & "entered " & Chr(32) & Target.Value & Chr(32) & "on " & Format(Now(), "mm/dd/yy")
Case Else
Exit Sub
End Select
CellComment.Text "User: " & Join(aryVals, ",")
Else
CellComment.Text "User: " & Environ("Username ") & Chr(32) & "entered " & Chr(32) & Target.Value & Chr(32) & "on " & Format(Now(), "mm/dd/yy") & ", " & _
Mid(CellComment.Text, InStr(1, CellComment.Text, ":") + 1)
End If
End If
Else
Target.AddComment "User: " & Environ("Username ") & Chr(32) & "entered " & Chr(32) & Target.Value & Chr(32) & _
"on " & Format(Now(), "mm/dd/yy")
End If
End If
End Sub
Excel 2003
Windows XP
Private Sub Worksheet_Change(ByVal Target As Range)
Dim CellComment As Comment
Dim CommentText As String
Dim aryVals As Variant
Dim i As Long
If Target.Count = 1 Then
Set CellComment = Target.Comment
If Not CellComment Is Nothing Then
If InStr(1, CellComment.Text, "User: ") > 0 Then
If InStr(1, Mid(CellComment.Text, InStr(1, CellComment.Text, ":") + 1), ",") Then
aryVals = Split(Mid(CellComment.Text, InStr(1, CellComment.Text, ":") + 1), ",")
Select Case Len(CellComment.Text) - Len(Replace(CellComment.Text, ",", vbNullString))
Case 1
aryVals = Array(Environ("Username ") & Chr(32) & "entered " & Chr(32) & Target.Value & Chr(32) & "on " & Format(Now(), "mm/dd/yy"), _
aryVals(0), _
aryVals(1))
Case 2
For i = UBound(aryVals) To LBound(aryVals) + 1 Step -1
aryVals(i) = aryVals(i - 1)
Next
aryVals(LBound(aryVals)) = Environ("Username ") & Chr(32) & "entered " & Chr(32) & Target.Value & Chr(32) & "on " & Format(Now(), "mm/dd/yy")
Case Else
Exit Sub
End Select
CellComment.Text "User: " & Join(aryVals, ",")
Else
CellComment.Text "User: " & Environ("Username ") & Chr(32) & "entered " & Chr(32) & Target.Value & Chr(32) & "on " & Format(Now(), "mm/dd/yy") & ", " & _
Mid(CellComment.Text, InStr(1, CellComment.Text, ":") + 1)
End If
End If
Else
Target.AddComment "User: " & Environ("Username ") & Chr(32) & "entered " & Chr(32) & Target.Value & Chr(32) & _
"on " & Format(Now(), "mm/dd/yy")
End If
End If
End Sub
Excel 2003
Windows XP