Solved

If Statement error

Posted on 2009-05-15
11
150 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

759 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

20 Experts available now in Live!

Get 1:1 Help Now