LoginSignup
1
1

More than 5 years have passed since last update.

エクセルでエポックタイムから日本時間(JST)に変換するマクロ

Posted at

処理としては、

  1. 選択範囲の右隣に列を追加
  2. 選択範囲のにあるエポックタイムをJSTに変換したものを隣の列に代入

を実施。以下ソース。


' エポックタイム⇒JSTへの変更を行うマクロ

' メイン処理
Sub main()

    ' 時間の高速化のための処理
    '' 更新処理停止
    ScreenUpdating = False
    '' 計算の自動化停止
    Calculation = xlCalculationManual

    ' エポックタイム⇒JSTへの変更
    Call doFormSelection

    ' 時間の高速化のための処理を解除
    Calculation = xlCalculationAutomatic
    ScreenUpdating = True

End Sub

' 選択範囲対する処理
Sub doFormSelection()

    Dim selectionRange As Range
    Dim selectionCell As Range
    Dim lastRow As Long

    lastRow = getLastRowRejectSplace(Selection)

    ' 選択範囲の右側にセルを追加
    Columns(Selection.Column + 1).Insert Shift:=xlToRight

    ' セルの書式を日付形式に変更
    Selection.Offset(0, 1).NumberFormatLocal = "yyyy/m/d h:mm;@"

    For Each selectionCell In Selection

       Call convertToJstFromEpoch(selectionCell)
       ' エポックタイム⇒JSTへの変更
       If selectionCell.Row = lastRow Then
            Exit For
       End If

    Next selectionCell

    ' 幅の調整
    Selection.Offset(0, 1).EntireColumn.AutoFit
End Sub

' エポックタイム⇒JSTへの変更
Sub convertToJstFromEpoch(targetCell As Range)

    Debug.Print TypeName(targetCell.Value)
    If TypeName(targetCell.Value) = "Double" Then
        targetCell.Offset(0, 1).FormulaR1C1 = "=(RC[-1]/(3600*24))+25569+135/360"
    End If

End Sub

' 引数で指定した範囲の最終行を取得
Function getLastRowRejectSplace(targetRange As Range) As Long
    getLastRowRejectSplace = Cells(Rows.Count, targetRange.Column).End(xlUp).Row
End Function
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1