Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 440
Default IF statement but in code

Hi,
I have code which will add a comment box two cells to the right whenever a
date is placed in column "N".
There is text within this comment box which is added by the code and takes
the date from "N" and adds 14 (days).
On occasion, I will need to overide the 14 days with a number from column
"M" which is manually input into that cell.

So, in eccence, I need someting like:
If (M5="",N5+14,N5+M5) but in the following code:

Dim strTemp As Date
'adds a comment box to cells in column P when date is entered in N
On Error Resume Next
If Not Intersect(Target, Me.Range("N4:N9999")) Is Nothing Then
With Target
If .Value < "" Then
Application.EnableEvents = False
strTemp = Target.Offset(0, 3).Text
Target.Offset(0, 2).AddComment UserName() & " - AoS due by: " &
Format(Target.Value + 14, "dd mmm")
Target.Offset(0, 2).Comment.Shape.TextFrame.AutoSize = True
'On Error GoTo 0
Application.EnableEvents = True
End If
End With
End If


Any ideas???
--
Traa Dy Liooar

Jock
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default IF statement but in code

Try the below

If Trim(Target.Offset(0,-1)) = "" Then
Target.Offset(0, 2).AddComment UserName() & " - AoS due by: " & _
Format(Target.Value + 14, "dd mmm")
Else
Target.Offset(0, 2).AddComment UserName() & " - AoS due by: " & _
Format(Target.Value + Target.Offset(0,-1).Value , "dd mmm")
End If

If this post helps click Yes
---------------
Jacob Skaria


"Jock" wrote:

Hi,
I have code which will add a comment box two cells to the right whenever a
date is placed in column "N".
There is text within this comment box which is added by the code and takes
the date from "N" and adds 14 (days).
On occasion, I will need to overide the 14 days with a number from column
"M" which is manually input into that cell.

So, in eccence, I need someting like:
If (M5="",N5+14,N5+M5) but in the following code:

Dim strTemp As Date
'adds a comment box to cells in column P when date is entered in N
On Error Resume Next
If Not Intersect(Target, Me.Range("N4:N9999")) Is Nothing Then
With Target
If .Value < "" Then
Application.EnableEvents = False
strTemp = Target.Offset(0, 3).Text
Target.Offset(0, 2).AddComment UserName() & " - AoS due by: " &
Format(Target.Value + 14, "dd mmm")
Target.Offset(0, 2).Comment.Shape.TextFrame.AutoSize = True
'On Error GoTo 0
Application.EnableEvents = True
End If
End With
End If


Any ideas???
--
Traa Dy Liooar

Jock

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default 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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default IF statement but in code

Jock,

Drop the line MsgBox "IsDate"
I used it during testing.

--
Regards,

OssieMac

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA code with and / or statement Steve Excel Programming 3 January 31st 08 11:47 PM
Help Identifying code statement Ayo Excel Discussion (Misc queries) 1 November 29th 07 03:50 AM
If statement to replace #VALUE! code jeannie v Excel Worksheet Functions 8 June 27th 07 03:53 AM
VBA code for IF statement with OR? Dave R. Excel Programming 4 June 27th 05 03:38 PM
Code doesn't go past first IF statement keepITcool Excel Programming 2 July 20th 04 09:48 PM


All times are GMT +1. The time now is 02:52 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"