0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

入力規則を設定しているセルに、ダブルクリックで指定した値を挿入するマクロ

Posted at

作業をする際に、時間名前チャックマークなど入力することがちょこちょこありまして、マウスで対象セルをダブルクリックするだけで、値を入力したり、消したりしたいなーという思いで、作り始めました。

作りとしては、
ダブルクリックしたセルに入力規則が設定されている確認して、なければ何もせず。
入力規則が設定されている場合は、Validation.Typeつまり入力規則の種類でIF分岐させる作りです。

Validation.TypexlValidateCustomにしてかつ、Target.Cells(1, 1).Validation.Formula1 = "任意の値"でカスタム値の値を取得して、IF分岐させれば、理論上なんでも好きなようにできちゃいます。

ひとまず、入力規則を時刻に設定しているパターンで、時刻を挿入したり、消したりするマクロを下記に書き記しておきます。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    '=================================================================================================================
    '①入力規則が設定されているか調べる機能
    Dim t As Long
    t = 0
    On Error Resume Next
    t = Selection.Item(1).Validation.Type
    On Error GoTo 0
    Debug.Print t
    If t > 0 Then '入力規則が設定されているときのみ、下記のマクロ実行
    '=================================================================================================================
        If Target.Cells(1, 1).Validation.Type = xlValidateTime Then  '②-① 入力規則が「時刻」である場合
             '【セルの値が空欄の時は現在時刻を代入し、それ以外の場合は値削除する】
            If Target.Cells(1, 1) = "" Then
                Target.Value = Now
            Else
                Target.ClearContents
            End If
    '=================================================================================================================
        Else  '②-END 入力規則が設定されているが条件に一致しなかった場合
            ' 何もしない
        End If
    End If
End Sub
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?