Hello,
I would like to get some grip on the syntax of a formula in excel 2007.
Logic is I want C2 to = "yes" if
B2's time is between 2 and 4 hours after A2's time.
Any help is much appreciated.
Hello,
I would like to get some grip on the syntax of a formula in excel 2007.
Logic is I want C2 to = "yes" if
B2's time is between 2 and 4 hours after A2's time.
Any help is much appreciated.
Assuming the times will be on the same day then try this
=IF(AND((B2-A2)*24>=2,(B2-A2)*24<=4),"Yes","No")
Audere est facere
Hello,
I keep getting a "missing then or go to" message:
I'm doing it in VBA though, and the code is:
I'm getting syntax error.![]()
If Cells(Target.Row, 2).Value >= 4 Then If DateDiff("n", Cells(Target.Row, 1).Value, Cells(Target.Row, 3).Value) <= 30 And DateDiff("h", Cells(Target.Row, 3).Value, Cells(Target.Row, 4).Value) >= 2 And DateDiff("h", Cells(Target.Row, 3).Value, Cells(Target.Row, 4).Value) <= 4, "Yes","No") then Cells(Target.Row, 5).Value = "Yes" Cells(Target.Row, 5).Value = "No"
Last edited by Lifeseeker; 11-22-2011 at 06:22 PM.
Can anybody help on this please?
Minor correction:
The logic is:
If B2 is >=4, E2 will = "Yes" only if (C2 - A2) <=30 is true AND D2's time is between 2 and 4 hours after C2.
Otherwise, E2 will say "No"
Hi Lifeseeker
This is the entire code in your workbookAs you can see...there's a bit missing. There's an![]()
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column > 35 Then Exit Sub Application.EnableEvents = False If Cells(Target.Row, 2).Value >= 4 Then If DateDiff("n", Cells(Target.Row, 1).Value, Cells(Target.Row, 3).Value) <= 30 And DateDiff("h", Cells(Target.Row, 3).Value, Cells(Target.Row, 4).Value) >= 2 And DateDiff("h", Cells(Target.Row, 3).Value, Cells(Target.Row, 4).Value) <= 4, "Yes","No") then Cells(Target.Row, 5).Value = "Yes" Cells(Target.Row, 5).Value = "No" End If Application.EnableEvents = True
missing and an![]()
End If
missing...not to mention...there's a reference to an "n" that's not defined. There's no way to trouble shoot this...not enough information...or code.![]()
End Sub
John
If you have issues with Code I've provided, I appreciate your feedback.
In the event Code provided resolves your issue, please mark your Thread as SOLVED.
If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.
Hi,
How about this version? I get no syntax error anymore, but it doesn't do the trick.
the logic is:![]()
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column > 35 Then Exit Sub Application.EnableEvents = False If Cells(Target.Row, 2).Value >= 4 And DateDiff("n", Cells(Target.Row, 1).Value, Cells(Target.Row, 3).Value) <= 30 And DateDiff("h", Cells(Target.Row, 3).Value, Cells(Target.Row, 4).Value) >= 2 And DateDiff("h", Cells(Target.Row, 3).Value, Cells(Target.Row, 4).Value) <= 4 Then Cells(Target.Row, 5).Value = "Yes" Else Cells(Target.Row, 5).Value = "No" End If Application.EnableEvents = True End If End Sub
If B2 is >=4, E2 will = "Yes" only if (C2 - A2) <=30 is true AND D2's time is between 2 and 4 hours after C2. (this is an example of a row-level calculation)
"n" is time difference displayed in minutes?
Thanks
hello
may be this approach
=IF(HOUR(A1-B1)>HOUR(2),"yes","")
try this also:
=IF(OR(HOUR(A1-B1)>2,HOUR(A1-B1)<=4),"yes","No")
Lifeseeker,
Attached is a modified version of your example workbook. Cell C2 didn't have an actual date because the "pm" was immediately next to the "12:30". VBA doesn't care, but formulas do. So here's VBA code for a Worksheet_Change event to perform as requested, and it will convert all dates entered incorrectly (like "7/4/11 12:30pm") to correct dates ("7/4/11 12:30:00 PM") and then check if E should be Yes or No in accordance to your logic stated above:
![]()
Private Sub Worksheet_Change(ByVal Target As Range) Dim rngChg As Range: Set rngChg = Intersect(Me.Columns("A:D"), Target) If rngChg Is Nothing Then Exit Sub Dim DateCell As Range Application.EnableEvents = False For Each DateCell In Intersect(rngChg.EntireRow, Union(Me.Columns("A"), Me.Columns("C:D"))) If IsDate(DateCell.Value) Then DateCell.Value = CDate(DateCell.Value) Next DateCell With Intersect(rngChg.EntireRow, Me.Columns("E")) .Formula = "=IF(AND(B" & .Row & ">=4,C" & .Row & "-A" & .Row & "<=30,AND(HOUR(D" & .Row & "-C" & .Row & ")>=2,HOUR(D" & .Row & "-C" & .Row & ")<=4)),""Yes"",""No"")" .Value = .Value End With Application.EnableEvents = True End Sub
Last edited by tigeravatar; 11-28-2011 at 01:00 PM.
Hope that helps,
~tigeravatar
Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks