If Statement error

Hi,

I have a problem with an if statement, that when the elapsed time is equal to or less than 09:00:00 it should make the active cell read "4-8 Hours" except it skips it  What am I doing wrong?
Dim Elapsed_Time As Date
Elapsed_Time = "09:00:00"
 
If Elapsed_Time <= "05:00:00" Then
Cells(ActiveCell.Row, 16).Value = "Under 4 Hours"
ElseIf Elapsed_Time <= "09:00:00" Then
Cells(ActiveCell.Row, 16).Value = "4-8 Hours"
Else
Cells(ActiveCell.Row, 16).Value = "Next Day"
End If

Open in new window

edmaceyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Chris BottomleyConnect With a Mentor Commented:
:o)
0
 
DhaestCommented:
I just tested here in a vba-function and it worked like a charm ...
0
 
edmaceyAuthor Commented:
I am using it in the Excel VBE and it doesn't work and doesnt work for me. What could the problem be?As it should work.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
DhaestCommented:
I just opened a new excel-application, with a new sheet and tested this function in vba...
sub test()
Dim Elapsed_Time As Date
Elapsed_Time = "09:00:00"
 
If Elapsed_Time <= "05:00:00" Then
Cells(ActiveCell.Row, 16).Value = "Under 4 Hours"
ElseIf Elapsed_Time <= "09:00:00" Then
Cells(ActiveCell.Row, 16).Value = "4-8 Hours"
Else
Cells(ActiveCell.Row, 16).Value = "Next Day"
End If
 
end sub

Open in new window

0
 
edmaceyAuthor Commented:
I have this if then else statement within a case statement and it just seems to miss it out when I change the Elapsed_Time to "09:00:01". Strange. Oh well thanks for the help anyway.
0
 
Chris BottomleyCommented:
Strictly speaking you are evaluating a date with a string, try:

Chris
Dim Elapsed_Time As Date
Elapsed_Time = "09:00:01"
 
If Elapsed_Time <= CDate("05:00:00") Then
Cells(ActiveCell.Row, 16).Value = "Under 4 Hours"
ElseIf Elapsed_Time <= CDate("09:00:00") Then
Cells(ActiveCell.Row, 16).Value = "4-8 Hours"
Else
Cells(ActiveCell.Row, 16).Value = "Next Day"
End If

Open in new window

0
 
edmaceyAuthor Commented:
Chris,

Tried what you suggested and it still doesn't work. It works as a separate sub but not in the code that I have (which is below)
Not sure if somewhere along the lines it is changing the format. Any help would be appreciated.
Dim StartTime As Date
Dim FinishTime As Date
Dim strStartTimes As Date
Dim strFinishTimes As Date
Dim Elapsed_Time As Date
 
StartTime = CDate(Cells(ActiveCell.Row, 1).Value)
FinishTime = CDate(Cells(ActiveCell.Row, 14).Value)
 
strStartTimes = CDate(Cells(ActiveCell.Row, 2).Value & " " & StartTime)
strFinishTimes = CDate(Cells(ActiveCell.Row, 15).Value & " " & FinishTime)
 
Number_of_WorkingDays = WorkingDays(strStartTimes, strFinishTimes)
Elapsed_Time = FinishTime - StartTime
 
Select Case Number_of_WorkingDays
 
Case 0
 
If Elapsed_Time <= CDate("05:00:00") Then
Cells(ActiveCell.Row, 16).Value = "Under 4 Hours"
ElseIf Elapsed_Time <= CDate("09:00:00") Then
Cells(ActiveCell.Row, 16).Value = "4-8 Hours"
Else
Cells(ActiveCell.Row, 16).Value = "Next Day"
End If

Open in new window

0
 
Chris BottomleyCommented:
Try as below with erroneous code removed

Select Case Number_of_WorkingDays
 
Case 0
 
Chris
Dim StartTime As Date
Dim FinishTime As Date
Dim strStartTimes As Date
Dim strFinishTimes As Date
Dim Elapsed_Time As Date
 
StartTime = CDate(Cells(ActiveCell.Row, 1).Value)
FinishTime = CDate(Cells(ActiveCell.Row, 14).Value)
 
strStartTimes = CDate(Cells(ActiveCell.Row, 2).Value & " " & StartTime)
strFinishTimes = CDate(Cells(ActiveCell.Row, 15).Value & " " & FinishTime)
 
Number_of_WorkingDays = WorkingDays(strStartTimes, strFinishTimes)
Elapsed_Time = FinishTime - StartTime
 
If Elapsed_Time <= CDate("05:00:00") Then
Cells(ActiveCell.Row, 16).Value = "Under 4 Hours"
ElseIf Elapsed_Time <= CDate("09:00:00") Then
Cells(ActiveCell.Row, 16).Value = "4-8 Hours"
Else
Cells(ActiveCell.Row, 16).Value = "Next Day"
End If

Open in new window

0
 
edmaceyAuthor Commented:
I have done as you suggested above and it still skips past the correct elapsed time.
0
 
Chris BottomleyCommented:
Are we talking a logic error here ... the elapsed time will be finish - start TIME so finish tomorrow of 08:00 minus start time today of 23:00 would be 23-8 or 15 hours etc?

Chris
0
 
edmaceyAuthor Commented:
I think I have found the error, it is because I am getting the Start and Finish times from an excel cell.
0
All Courses

From novice to tech pro — start learning today.