« 2013年2月 | トップページ | 2018年2月 »

2018年1月

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.

| | コメント (0) | トラックバック (0)

2018年1月 3日 (水)

1/10秒や1/100秒の時間計算

- English -

1/10秒や1/100秒単位の時間の計算をしようとしたら
案外難しいという話です。

セルに直接入力する方法では、
例えば「02:02.9」と入力すると
自動でセルの書式設定が「mm:ss.0」に変更されます。
(「02:02.23」では、セルの書式設定が「mm:ss.00」に変更される。)
ですので、意図したとおりの値が入力できます。
Vba20180103a

試しに「02:02.9」と入力したセルの書式設定を
「標準」に変更してみてください。
「02:02.9」と言う値は文字列ではなく
日付をあらわす小数の値「0.0014…」として入力されていることがわかります。
Vba20180103b

日付をあらわす小数の値については以下の記事を参照してください。
日時と時間

時間の値が意図したとおりに入力されているので
単純に「=A1-A2」と入力すれば差を計算できます。
(1/100秒単位も同様に計算できる。B列参照)
Vba20180103c

以上の操作をVBAでしてみます。
まず、「02:02.9」を「0.0014…」の値に変換するのに工夫が必要です。

秒単位までならTimeValue関数を使って

TimeValue("00:02:02")

とすればよいのですが、1/10秒や1/100秒単位ではうまくいきません。

TimeValue("02:02.9")

の値は2時間2分9秒になってしまいます。

ですので、秒までの時間はTimeValue関数を使い、
1/10秒や1/100秒は別で付加する方法で時間の計算をしていきます。

まず、

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

で2分2秒の部分を小数の値に変換します。

次に
1/10秒=1/24/60/60/10
0.9秒=9/10秒=9/24/60/60/10 なので

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

で0.9秒部分の小数の値が得られます。

以上をまとめたコードがこちら

1/10秒単位の時間を作成するコード:

Sub macro180103a()
'時間の計算
'1/10秒単位

    Dim MyTime As String
    MyTime = "02:02.9"
   
    Cells(1, 1) = "秒単位まで"
    Cells(2, 1) = "1/10秒"
    Cells(3, 1) = "合計"
   
    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

実行結果:
Vba20180103d

これを応用して
ある時間(MyTime1)とある時間(MyTime2)の
1/10秒単位の時間差を計算します。

コードはこちら

1/10秒単位の時間の差を計算するコード:

Sub macro180103b()
'時間の計算2
'1/10秒単位

    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

実行結果:
Vba20180103e

折角なので1/10秒単位の時間差を計算する関数を
上記コードを改造して作っておきます。

コードはこちら

1/10秒単位の時間の差を計算する関数のコード:

Sub macro180103c()
'時間の計算3
'1/10秒単位

    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
'時間の計算3
'1/10秒単位
'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

macro180103cの実行結果:
Vba20180103f

1/100秒単位の計算の関数も作ってみました。
1/10秒単位との違いを探してみてください。

1/100秒単位の時間の差を計算する関数のコード:

Sub macro180103d()
'時間の計算4
'1/100秒単位

    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
'時間の計算4
'1/100秒単位
'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

macro180103dの実行結果:
Vba20180103g

VBAで計算するのが手間だと感じる人は
シートに値を入力して
シート内で計算をして
計算結果をシートから取り出して利用する方法もありです。

| | コメント (0) | トラックバック (0)

« 2013年2月 | トップページ | 2018年2月 »