hiko-blog

VBA業務改善

MENU

時間計測 サンプル

Sub Main()
    Dim executionTime As Double
    executionTime = MeasureExecutionTime()
    
    MsgBox "計測完了" & vbLf & "実行時間は" & Format(executionTime, "0.000秒") & "でした"
End Sub
Function MeasureExecutionTime() As Double
    Dim startTime As Double
    startTime = Timer
    
    ' ここに測定したい処理を挿入
    Call AddSheetNamesToLastColumn2
    
    MeasureExecutionTime = Timer - startTime
End Function

 

Sub AddSheetNamesToLastColumn2()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim lastCol As Long


    ' 各シートに対して処理を実行
    For Each ws In ThisWorkbook.Worksheets
        ' 最終行と最終列を取得
        lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
        
        ' タイトル行の一番右の列にシート名を入れる
        ws.Cells(1, lastCol + 1).Value = "シート名"
        
        ' 最終行までシート名をコピー
        ws.Cells(2, lastCol + 1).Resize(lastRow - 1, 1).Value = ws.Name
    Next ws
    
End Sub