IF statement but in code
Hello again Jock,
I assumed that it is a worksheet change event. Hope that is correct.
Not sure if Username is meant to be a variable that you created or the
Username of the workstation. I made it the username of the workstation.
Also it is preferrable to put the Application.EnableEvents = False at the
start of your code and re-enable at the end of the code. Plus it is advisable
to have the following code somewhere handy in the module so that if your code
fails at any time during testing before it gets to the code to re-enable.
Once events is turned off they stay off until restart Excel or enable with
code. Following sub can be run from within VBA editor by placing cursor
anywhere in the sub and press F5.
Sub Re_EnableEvents()
Application.EnableEvents = True
End Sub
Dim strTemp As Date does not look right considering where it is used in the
code. Is it a date or a string?
Anyway the following code should work but you might need to adjust the
Username if you are setting Username somewhere else in your code. However, if
you are setting it then change the variable to something other then Username
because it is a reserved word.
Note that a space and underscore at the end of a line is a line break in an
otherwise single line of code.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim strTemp As Date
'adds a comment box to cells in column P when date is entered in N
Application.EnableEvents = False
If IsDate(Target) Then 'Test if valid date.
MsgBox "IsDate"
On Error Resume Next
If Not Intersect(Target, Me.Range("N4:N9999")) Is Nothing Then
With Target
If .Value < "" Then
strTemp = Target.Offset(0, 3).Text
Target.Offset(0, 2).AddComment Application.UserName _
& " - AoS due by: " & _
Format(Target.Value + 14, "dd mmm")
Target.Offset(0, 2).Comment.Shape. _
TextFrame.AutoSize = True
End If
End With
End If
End If
Application.EnableEvents = True
End Sub
--
Regards,
OssieMac
|