Solved

If Statement error

Posted on 2009-05-15
11
152 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

803 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