checking to see if cell time value is in range

Prevost

Board Regular
Joined
Jan 23, 2014
Messages
198
Hi There. I have a large column of cells that contain a time value (just the time in HH:MM) and I would like to delete all the cell rows that are not in the time range from 8:00AM to 5:00PM. I am struggling with checking time values through VBA and checking to see if the time falls in a certain range.

I wanted to use something like:

If 5:00PM < cell < 11:59PM OR 12:00AM < cell < 8:00AM then
(delete cell row)
end if

I don't know how to check for the time range of the cell.

Any help or suggestions are appreciated. Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
.
.


Assuming your time values are in column A, then you could try this:


Code:
Sub DeleteRows()

  Dim rngUsedCells   As Range
  Dim rngUsedCell    As Range
  Dim dblTimeDec     As Double
  Dim alngDelRows()  As Long
  Dim lngDelRowCount As Long
  Dim lngCounter     As Long
  
  Set rngUsedCells = Intersect(ActiveSheet.UsedRange, Columns("A"))
  
  For Each rngUsedCell In rngUsedCells
    
    If IsDate(rngUsedCell.Value) Then
      dblTimeDec = rngUsedCell.Value - Int(rngUsedCell.Value)
      
      If dblTimeDec < (8 / 24) Or dblTimeDec >= (17 / 24) Then
        lngDelRowCount = 1 + lngDelRowCount
        ReDim Preserve alngDelRows(1 To lngDelRowCount)
        alngDelRows(lngDelRowCount) = rngUsedCell.Row
      End If
    
    End If
  
  Next rngUsedCell
  
  For lngCounter = lngDelRowCount To 1 Step -1
    Rows(alngDelRows(lngCounter)).Delete
  Next lngCounter

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,580
Members
449,089
Latest member
Motoracer88

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