+ Reply to Thread
Results 1 to 6 of 6

Calculate Time per Period: Type Mismatch

Hybrid View

  1. #1
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Calculate Time per Period: Type Mismatch

    Hi guys,

    I am trying to calculate and add up the total time for a specific date period.

    But I get a Run-time error: 13 - Type Mismatch

    On this line:

    WorkShift = WorkShift + Scell.Offset(0, 1)
    i have attached a sample workbook.

    Thank you in advance for all your help!
    Attached Files Attached Files
    Last edited by onmyway; 03-15-2015 at 06:14 AM.
    Sharing knowledge, can be likened to taking another person's hand, and pulling them up to a higher level -- onmyway

    If I was helpful, please remember to click on * Add Reputation below

  2. #2
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Calculate Time per Period: Type Mismatch

    Hi all,

    i figured it out, but could still do with some help solving it:

    Not Working: The format in my cell is 12:47. When you click on it, it also show 12:47 in the Formula Bar.
    Working: The format in my cell is 12:47. When you click on it, it shows 12:47:00 PM in the Formula Bar.

    How can i edit this code to work either way?

    Thank you!

  3. #3
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Calculate Time per Period: Type Mismatch

    Quote Originally Posted by onmyway View Post
    Not Working: The format in my cell is 12:47. When you click on it, it also show 12:47 in the Formula Bar.
    Working: The format in my cell is 12:47. When you click on it, it shows 12:47:00 PM in the Formula Bar.
    How can i edit this code to work either way?
    The "times" in column C are text, not numeric time. To convert to numeric time, select C4:C18 and click on Data, Text To Columns, Finish.

    PS: The Formula Bar always displays numeric time in the form 12:47:00 PM, regardless of the cell format.

  4. #4
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Calculate Time per Period: Type Mismatch

    Hi joeu2004

    Thanks for the feedback. I realize the format issues (like you explained). I was hoping to do the formatting via VBA, and not in the sheet. I want users to stay away from the sheet and the data. Safeguard it as much as possible.

    This is the actual code I am using to format the time field to save it to the sheet in this format HH:MM

    'TIME FORMAT
    Private Sub txtWorkShiftStart_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        Dim tString               As String
        
    If Me.txtWorkShiftStart = "" Then
        MsgBox "Please enter a Work Start of Shift"
        Cancel = True
    End If
        
        With txtWorkShiftStart
            ' don't process if blank
            If Len(.Value) <> 0 Then
            
                'Check if user put in a colon or not
                If InStr(1, .Value, ":", vbTextCompare) = 0 Then
                    'If not, make string 4 digits and insert colon
                    tString = Format(.Value, "0000")
                    tString = Left(tString, 2) & ":" & Right(tString, 2)
    
                    If IsDate(tString) Then
                        txtWorkShiftStart.Value = Format(TimeValue(tString), "HH:MM")
                    Else
                        MsgBox "Not a valid time value"
                        Cancel = True
                    End If
                Else
                    If IsDate(.Value) Then
                        'Otherwise, take value as given
                        .Value = Format(.Value, "hh:mm")
                    Else
                        MsgBox "Not a valid time value"
                        Cancel = True
                    End If
                End If
                
            End If
            
        End With
    
    End Sub
    Perhaps any suggestions on how I can edit this to save in my desired format, but as numeric time?

    Thank you!

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,637

    Re: Calculate Time per Period: Type Mismatch

    Just change to
    WorkShift = WorkShift + TimeValue(Scell.Offset(0, 1))

  6. #6
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Calculate Time per Period: Type Mismatch

    Ah! Perfect, thank you!

+ 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. Run time error type mismatch 13
    By Dariusd7 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-15-2013, 05:11 PM
  2. [SOLVED] Time [type mismatch]
    By Neisany in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-10-2012, 01:53 PM
  3. [SOLVED] Complicated:Calculate time period based on no. of units sold + fluctuations within period
    By omaha.crab in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2012, 03:04 AM
  4. run-time 13 Type mismatch
    By simonsmith in forum Excel General
    Replies: 2
    Last Post: 05-18-2006, 11:14 AM
  5. Run-time error '13':Type mismatch
    By Sibilia in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-03-2005, 03:54 PM

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