+ Reply to Thread
Results 1 to 4 of 4

Thread: time returns a decimal value when searched

  1. #1
    Registered User
    Join Date
    07-31-2008
    Location
    United States
    Posts
    38

    time returns a decimal value when searched

    hello,

    I need your ideas xl xperts on this one. I have a UserForm with a find button. My worksheet contains time values. My question is, when I perform a search or find function, the values being returned on my UserForm from the worksheet are in the form of decimal values but not the actual time values as indicated on the worksheet. Why is that so? Anyone knows how to format the time to return time values as actual time values rather than decimal?

    Thanks,

    Burke

  2. #2
    Forum Guru VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    12,010
    Burke,

    If you format a cell with a time as general it will return a value like 0.0520833333333333 which is 01:15. This is because that how excel stores times.

    If you times 0.0520833333333333 by 24 and the cell is formatted as general it will return 1.25

    See link for more on time
    http://www.mvps.org/dmcritchie/excel/datetime.htm

    In vba you could use something like
    MsgBox Format(Range("A1").Value, "HH:MM")
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    07-31-2008
    Location
    United States
    Posts
    38
    Hello vba noob,

    Thanks for your reply. Here the code I was studying with. I got it from one of the threads here: How can I format the time to return as actual time like in my work sheet?

    Private Sub cmdadminfind_Click()
        Dim MatchCell As Range
        Dim strFind, FirstAddress As String   'what to find
        Dim rSearch As Range  'range to search
        Dim f As Integer
        
         'Search Range is the ID numbers column
          Set rSearch = ActiveSheet.Range("A1", Cells(Rows.Count, "A").End(xlUp))
          
         'ID number to find
          strFind = TextBox3.Value
          
          'Allways set All the Named Argumnets for the First Search.
          'The system "Find" function uses these settings also.
            With rSearch
              Set MatchCell = .Find(What:=strFind, _
                                              After:=Cells(1, 1), _
                                              LookIn:=xlValues, _
                                              LookAt:=xlWhole, _
                                              SearchOrder:=xlRows, _
                                              SearchDirection:=xlNext, _
                                              MatchCase:=False)
                                              
                If Not MatchCell Is Nothing Then    'found it
                   'load entry to form
                    TextBox10.Value = MatchCell.Offset(0, 1).Value    'Employee name
                    TextBox4.Value = MatchCell.Offset(0, 5).Value      'Time 1
                    TextBox5.Value = MatchCell.Offset(0, 6).Value      'Time 2
                    TextBox6.Value = MatchCell.Offset(0, 8).Value      'Time 3
                    TextBox7.Value = MatchCell.Offset(0, 9).Value      'Time 4
                    TextBox8.Value = MatchCell.Offset(0, 11).Value     'Time 5
                    TextBox9.Value = MatchCell.Offset(0, 12).Value     'Time 6
                  
                  FirstAddress = MatchCell.Address
                  CurRow = MatchCell.Row
                
                 'Check for and count duplicate entries
                  Do
                    f = f + 1    'count number of matching records
                    Set MatchCell = .FindNext(MatchCell)
                  Loop While Not MatchCell Is Nothing And MatchCell.Address <> FirstAddress
                
                  If f > 1 Then
                    MsgBox "There are " & f & " instances of " & strFind
                    Me.Height = 318
                  End If
                Else
                  MsgBox strFind & " not listed"    'search failed
                End If
            End With
    End Sub
    Thanks,

    Burke

  4. #4
    Registered User
    Join Date
    07-31-2008
    Location
    United States
    Posts
    38
    hello,

    I was able to format the correct time values.

    I changed the code from:

     TextBox4.Value = MatchCell.Offset(0, 5).Value      'Time 1
                    TextBox5.Value = MatchCell.Offset(0, 6).Value      'Time 2
                    TextBox6.Value = MatchCell.Offset(0, 8).Value      'Time 3
                    TextBox7.Value = MatchCell.Offset(0, 9).Value      'Time 4
                    TextBox8.Value = MatchCell.Offset(0, 11).Value     'Time 5
                    TextBox9.Value = MatchCell.Offset(0, 12).Value     'Time 6
    to

    TextBox4.Value = Format(MatchCell.Offset(0, 5).Value, "HH:MM:SS")
                    TextBox5.Value = Format(MatchCell.Offset(0, 6).Value, "HH:MM:SS")
                    TextBox6.Value = Format(MatchCell.Offset(0, 8).Value, "HH:MM:SS")
                    TextBox7.Value = Format(MatchCell.Offset(0, 9).Value, "HH:MM:SS")
                    TextBox8.Value = Format(MatchCell.Offset(0, 11).Value, "HH:MM:SS")
                    TextBox9.Value = Format(MatchCell.Offset(0, 12).Value, "HH:MM:SS")
    Thanks for giving me the idea vbanoob...Another solved thread from the xperts.

    Thanks,

    Burke
    Last edited by Burke; 08-06-2008 at 03:09 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0