Solved

If Statement error

Posted on 2009-05-15
11
151 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DO Loop not working 4 72
VB error "Type mismatch" 2 49
Crystal reports - Formula Field code need assistance with code 17 52
using web browser with BING 40 110
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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now