If Conditions on Time Values

j0x

New Member
Joined
Jun 12, 2011
Messages
8
hello im new here and trying hard in programming
im trying to program this pseudo code in VBA excel

Code:
reghrs
  if totalhrs >= 8 then 
     reghrs = 8
  else
     if ((totalhrs - 8) <= 0) then
        reghrs = 0
     else
	reghrs = roundup(totalhrs)
     end if
  end if

but so far im stuck with this non-working code

Code:
Sub RegHrs()
        With ActiveCell.Offset(0, 2)
            .NumberFormat = "0.00"
            If ActiveCell.Offset(0, 1).Value >= TimeSerial(8, 0, 0) Then
                    .Value = 8
            End If
        End With
End Sub

the If ActiveCell.Offset(0, 1).Value >= TimeSerial(8, 0, 0) Then is returning false so i guess i do not know how to properly apply If conditional statements to timevalues, so i need help, thanks for any help :)
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
This works satisfactory in my tests:

Code:
Sub RegHrs()
    With ActiveCell
        If .Offset(0, 1).Value >= TimeSerial(8, 0, 0) Then
            .Offset(0, 2).Value = 8
        Else
            .Offset(0, 2).Value = 0
        End If
    End With
End Sub

Wigi
 
Upvote 0
Code:
Sub RegHrs()
        With ActiveCell
            .Offset(0, 2).NumberFormat = "0.00"
            If .Offset(0, 1).Value >= TimeSerial(8, 0, 0) Then
                 .Offset(0, 2).Value = 8
            Else
                 .Offset(0, 2).Value = WorksheetFunction.RoundUp(.Offset(0, 1).Value, 0)
            End If
        End With
End Sub

i tried it but it always go to the else condition so the problem is still in this part -> If .Offset(0, 1).Value >= TimeSerial(8, 0, 0) Then
 
Upvote 0
J0x, Your code did not work for me until I places this line in code (with various Tmes) for testing.
You code then worked for various values.
Code:
ActiveCell.Offset(0, 1).value = TimeSerial(7, 0, 0)
I then removed the line and entered values in ".offset(,1)" manually, the code then worked.
The code also worked on other sheets and with other cell formats for .offset(,1).
Very strange !!!
 
Upvote 0
What is in that cell?

ActiveCell.Offset(0,1).Value contains time value like this 8:48:52 its formatcell is Time (13:35:50)


J0x, Your code did not work for me until I places this line in code (with various Tmes) for testing.
You code then worked for various values.
Code:
ActiveCell.Offset(0, 1).value = TimeSerial(7, 0, 0)
I then removed the line and entered values in ".offset(,1)" manually, the code then worked.
The code also worked on other sheets and with other cell formats for .offset(,1).
Very strange !!!

im looking to get the time values for ActiveCell.Offset(0,1) and to process it to the if condition, i cannot make the time value to be manually inputted :(

and can i see your complete code?
 
Upvote 0
lol the offset position is the culprit im actually processing the time 07:48:52 and not 8:48:52

thanks for the helps

P.S. and the edit button for the previous replies are gone when i come back here? so sorry for the double post
 
Upvote 0
This is your code slightly modified, which works for me.
To test I changed the Time value (In Red) to sometime before 08:00 and some time after 08:00.
Then selected "A1" , then ran the code, Which obviouly resulted in "C1" being "8:00" or nothing.

Rich (BB code):
Sub RegHrs()
        With ActiveCell.Offset(0, 2)
            .NumberFormat = "0.00"
             ActiveCell.Offset(0, 1).value = TimeSerial(4, 0, 0)
           If ActiveCell.Offset(0, 1).value >= TimeSerial(8, 0, 0) Then
                    .value = 8
                Else
                    .value = ""
            End If
        End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top