« 1/10秒や1/100秒の時間計算 | トップページ | セルの内容を90度回転、-90度回転、180度回転 »

2018年1月21日 (日)

Calculation of units 1/10 sec and 1/100 sec

It's difficult to calculate a time
When by 1/10 sec or 1/100 sec.

When you input a time value "02:02.9" into  a cell,
Excel change automatically the cell's format into "mm:ss.0So".
(In case "02:02.23", the cell's format is changed into "mm:ss.00")
So you can input the time as you will.I
Vba20180103a

If you change the cell's format "mmss.0" into "normal",
you know that "02:02.9" is equal to "0.0014...".
Vba20180103b

See also the article:Date And Time about a serial value of date.
Date And Time

After you input two time value into cells, input "=A1-A2" into A3 cell,
so you can get the difference of "02:02.9" and "02:02.5".
(1/100 sec is same way. See B column.)
Vba20180103c

The next is doing the same thing by VBA.
You need a technique  to change"02:02.9" into "0.0014...".

You can use Timevalue Function to change Time by unit of sec.

TimeValue("00:02:02")

This doesn't work by unit of 1/10 sec or 1/100 sec.

TimeValue("02:02.9")

The above is 2 hour 2 min 9 sec.(not 2min 2sec 9/10 sec)

So you can use Timevalue Function for sec unit calculation,
and add the part of 1/10 sec or 1/100 sec by another method.

Then,

TimeValue("00:" & LEFT("02:02.9",5))

The above can convert 2min 2sec to dicimal value.

Next is
1/10 sec=1/24/60/60/10
0.9 sec=9/10 sec=9/24/60/60/10 therefore,

Right("02:02.9", 1) / 24 / 60 / 60 / 10

The above returns dicimal value of 0.9 sec.

Here is the code of the procedure.

Code of caluclation a time value by 1/10 sec.:

Sub macro180103a()
'Caluclate a time value
'By 1/10 sec

    Dim MyTime As String
    MyTime = "02:02.9"
   
    Cells(1, 1) = "sec"
    Cells(2, 1) = "1/10sec"
    Cells(3, 1) = "total"
   
    Cells(1, 2) = TimeValue("00:" & Left(MyTime, 5))
    Cells(2, 2) = Right(MyTime, 1) / 24 / 60 / 60 / 10
    Cells(3, 2) = CDbl(TimeValue("00:" & Left(MyTime, 5)) _
        + Right(MyTime, 1) / 24 / 60 / 60 / 10)
    Range ("B1:B3").NumberFormatLocal = "mm:ss.0"
   
End Sub

Result:
Vba20180103d

You can apply "macro180103"
to subtract a time values : MyTime1 - MyTime2 by unit of 1/10 sec.

Here is the code.

Code of subtraction a time value by unit of 1/10 sec:

Sub macro180103b()
'Caluclate a time value2
'By 1/10 sec

    Dim MyTime1 As String, MyTime2 As String
    MyTime1 = "02:02.9"
    MyTime2 = "02:02.5"
   
    Cells(1, 1) = "MyTime1"
    Cells(2, 1) = "MyTime2"
    Cells(3, 1) = "MyTime1-MyTime2"
   
    Cells(1, 2) = MyTime1
    Cells(2, 2) = MyTime2
    Cells(3, 2) = CDbl(TimeValue("00:" & Left(MyTime1, 5)) _
        + Right(MyTime1, 1) / 24 / 60 / 60 / 10) _
        - CDbl(TimeValue("00:" & Left(MyTime2, 5)) _
        + Right(MyTime2, 1) / 24 / 60 / 60 / 10)
   
    Range("B1:B3").NumberFormatLocal = "mm:ss.0"
   
End Sub

Result:
Vba20180103e

 

In addition, you can reuse macro180103b
to make a Function that subtract a time value by unit of 1/10 sec.

Here is the code.

Function of subtraction a time value by unit of 1/10 sec:

Sub macro180103c()
'Caluclate a time value3
'By 1/10 sec

    Cells(1, 1) = TimeCul10("02:02.9", "02:02.5")
   
    Range("A1").NumberFormatLocal = "mm:ss.0"
   
End Sub
Function TimeCul10(MyTime1 As String, MyTime2 As String) As Double
'Caluclate a time value3
'By 1/10 sec
'MyTime1 > MyTime2
'TimeCul10 = MyTime1 - MyTime2

    TimeCul10 = CDbl(TimeValue ("00:" & Left(MyTime1, 5)) _
        + Right(MyTime1, 1) / 24 / 60 / 60 / 10) _
        - CDbl(TimeValue("00:" & Left(MyTime2, 5)) _
        + Right(MyTime2, 1) / 24 / 60 / 60 / 10)

End Function

Result of macro180103c:
Vba20180103f

And then, I made a Function
that subtracts timevalues by unit of 1/100 sec.
Check differences between the the code for 1/10 sec and the code for 1/100 sec.

Function of subtraction a time value by unit of 1/100 sec:

Sub macro180103d()
'Caluclate a time value4
'By 1/100 sec

    Cells(1, 1) = TimeCul100("02:02.23", "02:02.11")
   
    Range("A1").NumberFormatLocal = "mm:ss.00"
   
End Sub
Function TimeCul100(MyTime1 As String, MyTime2 As String) As Double
'Caluclate a time value4
'By 1/100 se
'MyTime1 > MyTime2
'TimeCul100 = MyTime1 - MyTime2

    TimeCul100 = CDbl(TimeValue("00:" & Left(MyTime1, 5)) _
        + Right(MyTime1, 2) / 24 / 60 / 60 / 100) _
         - CDbl(TimeValue("00:" & Left(MyTime2, 5)) _
         + Right(MyTime2, 2) / 24 / 60 / 60 / 100)

End Function

Result of macro180103d:
Vba20180103g

If you feel difficult to calculate timevalues by VBA,
you can use a worksheet as easier way.
Input timevalues into cells and calcuate in the cells,
then use the result value by VBA.

|

« 1/10秒や1/100秒の時間計算 | トップページ | セルの内容を90度回転、-90度回転、180度回転 »

コメント

この記事へのコメントは終了しました。

トラックバック


この記事へのトラックバック一覧です: Calculation of units 1/10 sec and 1/100 sec:

« 1/10秒や1/100秒の時間計算 | トップページ | セルの内容を90度回転、-90度回転、180度回転 »