+ Reply to Thread
Results 1 to 6 of 6

Time query to find number of times in a certain range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2013
    Location
    Los angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    109

    Time query to find number of times in a certain range

    Hello all,

    So I am trying to devise a code that will allow a user to input a range of times via dialog box. Then with this range (a startTime and endTime), the VBA code will search the worksheet and count the number of trips that take place in that time interval. I can't figure out a way to convert the user entry into a h:mm AM/PM format but I feel like I am on the right track.

    If anyone could take a look at my attached code/file and steer me in the right direction, that would be awesome! I would also be happy to clarify anything I left out, thank you.

    -Chad

    
    Dim count1 as integer
    Set pullinspullouts1 = Worksheets("Pull-ins & Pull-outs").Range("B4", Range("B" & Rows.Count).End(xlUp))
    
    'Convert text to time
    fromTime.NumberFormat = "h:mm AM/PM"
    toTime.NumberFormat = "h:mm AM/PM"
            
    End If
    
    For Each cell In pullinspullouts1
        If InStr(cell.Text, "x") > 0 Then
            n = Len(Trim(cell))
            cellval = Left(Trim(cell), n - 5) & ":" & Left(Right((Trim(cell)), 4), 2) & " AM"
            cell.NumberFormat = "h:mm AM/PM"
        End If
        
        
        If cell.Value >= #fromTime# And cell.Value < #toTime# Then
                count1 = count1 + 1
        End If
        
        MsgBox "There are " & count1 & "trips from " & fromTime & "to " & toTime
    timequery.xlsm

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Time query to find number of times in a certain range

       If cell.Value >= #fromTime# And cell.Value < #toTime# Then
    fromTime is not defined as a cell or range. You need to format it a cell or range first before you test it with if.

  3. #3
    Forum Contributor
    Join Date
    07-12-2013
    Location
    Los angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    109

    Re: Time query to find number of times in a certain range

    count1 = 0
    For Each cell In worktotalDXdept1
    cell.NumberFormat = "h:mm AM/PM"
        If InStr(cell.Text, "x") > 0 Then
            n = Len(Trim(cell))
            cellval = Left(Trim(cell), n - 5) & ":" & Left(Right((Trim(cell)), 4), 2) & " AM"
            If cellval >= fromTime And cellval < toTime Then
                count1 = count1 + 1
            End If
        End If
        
        If Not InStr(cell.Text, "x") > 0 Then
            If cell.Text >= fromTime And cell.Text < toTime Then
                count1 = count1 + 1
            End If
        End If
    Next
    
        MsgBox "There are " & count1 & " trips from " & fromTime & " to " & toTime & " ."
    This code works for one column of times, however when I enter a range of times outside the specified row, count1 always returns "0" trips. How can I have it search multiple columns of time?

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Time query to find number of times in a certain range

    May be this line if worktotalDXdept1 is a named range

    For Each cell In worktotalDXdept1

  5. #5
    Forum Contributor
    Join Date
    07-12-2013
    Location
    Los angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    109

    Re: Time query to find number of times in a certain range

    The number of items in it's column will always vary, so I'm not sure that would work out the way I'm hoping. I am trying to define fromTime and toTime as a range, but since these variables refer to the string of text from the userform's text box, I'm kind of confused how that would work. I feel like I'm so close to getting this thing right. Am I headed in the right direction? Still getting an error (Expected: Expression) with this:

    Range("A2000").Formula = fromTime
    Range("A2001").Formula = toTime
    
    Range("A2000").NumberFormat = "h:mm AM/PM"
    Range("A2001").NumberFormat = "h:mm AM/PM"
    
    If cellval >= #Range("A2000").value# And cellval <= #Range("A2000").value# Then
    Thank you for your help so far, AB33. Please let me know if you need any additional info. Please refer to this updated file: Book4.xlsm
    Last edited by aloha31; 08-13-2013 at 04:44 PM.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Time query to find number of times in a certain range

    Sorry!
    I can not test it, but you need to remove the hashes from these lines
     If cellval >= Range("A2000").Value And cellval <= Range("A2000").Value Then
    You may putin dates in hashes but not any other.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] find number of times a letter or a number appears in a column
    By dcoates in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-17-2012, 02:47 PM
  2. Query around time formats and addition of times with large numbers
    By Grimace in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-05-2010, 04:10 AM
  3. Replies: 6
    Last Post: 03-02-2010, 04:15 PM
  4. Replies: 1
    Last Post: 04-08-2009, 05:24 AM
  5. Time (clock) times a number= very high number?
    By saadeet in forum Excel General
    Replies: 2
    Last Post: 04-02-2009, 11:24 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1