Dealing with Time Values

amaglam

New Member
Joined
Dec 11, 2020
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Hi, would someone please help me understand why my column "G" that has a custom value set to "h:mm:ss" has a VarType of double? I am trying to filter for rows that fall between my start hour and end hour, but I cant seem to get the types to match. Looking at a cell value in column "G", it shows "23:00:00", and upon clicking into the cell it shows "11:00:00 PM". I've tried man different variations with TimeSerial as but can't seem to get them to match.

VBA Code:
                    dtStartTime = Format(TimeSerial(intStartHour, 0, 0), "h:mm:ss")
                    dtEndTime = Format(TimeSerial(intEndHour, 0, 0), "h:mm:ss")
                  
                    .AutoFilter Field:=7, Criteria1:=">" & dtEndTime, Operator:=xlAnd, Criteria2:="<=" & dtStartTime
                    .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
                    shData.AutoFilterMode = False
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
These 2 methods works for me:
VBA Code:
Sub try1()
Dim x As Double, y As Double
x = Range("C1")
y = Range("D1")
Range("A1").AutoFilter Field:=1, Criteria1:=">" & x, Operator:=xlAnd, Criteria2:="<" & y

End Sub


Sub try2()
Dim x As Double, y As Double
x = TimeValue("13:30:11")
y = TimeValue("17:00:01")
Range("A1").AutoFilter Field:=1, Criteria1:=">" & x, Operator:=xlAnd, Criteria2:="<" & y

End Sub

Book1
ABCD
1data13:30:1117:00:01
211:00:00
312:00:00
413:00:00
514:00:00
615:00:00
716:00:00
817:00:00
918:00:00
Sheet2


both have the same result:
Book1
ABCD
1data13:30:1117:00:01
514:00:00
615:00:00
716:00:00
817:00:00
10
Sheet2
 
Upvote 0
:/ still doesnt like it

the data in column "G" show 0:00:05 but when i click into it its 12:00:05 AM
 
Upvote 0
1. Do you mean the cell shows 0:00:05 and in formula bar shows 12:00:05 AM? or is it the other way around?
2. Can you post a sample data?
3. How does intStartHour get its value?
 
Upvote 0
I change the time format to "hh:mm:ss AM/PM", and both codes still works:
Book1
ABCD
1data01:30:11 PM05:00:01 PM
211:00:00 AM
312:00:00 PM
401:00:00 PM
502:00:00 PM
603:00:00 PM
704:00:00 PM
805:00:00 PM
906:00:00 PM
10
Sheet2


Book1
ABCD
1data01:30:11 PM05:00:01 PM
502:00:00 PM
603:00:00 PM
704:00:00 PM
805:00:00 PM
10
Sheet2
 
Upvote 0
The cell value is 0:00:05. I set intStartHour as an Integer
 
Upvote 0
A time value is always a decimal so you definitely don't want it as an Integer. You also don't really want to be using Format as it is a string function.
 
Last edited:
Upvote 0
The reason I used intStartHour was because I do a check to see if its between the start time and end time, and I though I would be able to use TimeSerial to change both column G and my intStartHour. So are you guys saying that I should declare a new variable and reference intStartHour?
 
Upvote 0
The way you are using intStartHour (just as a digit within TIMESERIAL) is fine as an Integer, if you were using it as an actual time then it wouldn't be.
Looking at your original code are you sure you have the criteria the right way around? If you want to filter the times between surely you want it > the start time and < the end time?
 
Upvote 0
Is it not valid to use .NumberFormat = "h:mm:ss" on Column G and set my variables like I had them?

VBA Code:
 dtStartTime = Format(TimeSerial(intStartHour, 0, 0), "h:mm:ss")
dtEndTime = Format(TimeSerial(intEndHour, 0, 0), "h:mm:ss")
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,449
Members
449,160
Latest member
nikijon

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