Solved

If Statement error

Posted on 2009-05-15
11
153 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb6 programatically bring window to front 8 76
Windows Script Host failed (Access is denied.) error 6 235
VBA filters 2 67
VBA: Select SQL query based on a config Sheet v2 11 49
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

830 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