Solved

If Statement error

Posted on 2009-05-15
11
156 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:edmacey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 24394068
I just tested here in a vba-function and it worked like a charm ...
0
 

Author Comment

by:edmacey
ID: 24394097
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 24394104
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:edmacey
ID: 24394177
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24394678
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
 

Author Comment

by:edmacey
ID: 24394874
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24394955
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
 

Author Comment

by:edmacey
ID: 24395105
I have done as you suggested above and it still skips past the correct elapsed time.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24395259
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
 

Author Comment

by:edmacey
ID: 24395333
I think I have found the error, it is because I am getting the Start and Finish times from an excel cell.
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 50 total points
ID: 24395412
:o)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 5 hours left to enroll

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question