ばくがの業務効率化チャンネル

このブログは個人的なエクセルの備忘録です

ダブルクリックでセルに値を入れる

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    If Not Intersect(Target, Range("B2:C19")) Is Nothing Then
        Cancel = True
        Target.Formula = Date
    End If
    
     'E列のセルをダブルクリックした場合
    If Target.Column = 5 Then
        'D列の最終行を取得
        Dim lastRow As Long
        lastRow = Cells(Rows.Count, "D").End(xlUp).Row
        'E列でクリックしたセルがD列の最終行と同じの場合には何もしない
        If Target.Row <> lastRow Then
            'セルの値をコピー
            Target.Copy
            'E列の空白のセルにペースト
            Range("E3:E" & lastRow).SpecialCells(xlCellTypeBlanks).PasteSpecial xlPasteValues
            'コピーを解除
            Application.CutCopyMode = False
        End If
        'ダブルクリックのデフォルト動作をキャンセル
        Cancel = True
    End If
    
End Sub