Restrict Event Code to Column

  • Hi


    Can you please point me in the right direction.


    I have a simple piece of code that enters a date value into the active cell when I click on a cell:

    Code
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If ActiveCell.Value = "" Then
    ActiveCell.Value = Range("B1").Value & " - "
    Else
    ActiveCell.Value = ActiveCell.Value & Chr(10) & Range("B1").Value & " - "
    
    
    End If
    End Sub


    I would like to restrict the code to the active cells in column 1 (column B - no headers).


    Is Chr(10) the right equivalent for Alt Enter?


    Any improvement to the code would be welcome together with suggestions on how to restrict the code to column B.


    Than you


    Jean

  • Re: Restrict Text Entry To Column


    Code
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column = 2 Then '2=column B
     Cancel = True 'saves you pressing enter, if you want to continue editing the cell remove this line
     If ActiveCell.Value = "" Then
      ActiveCell.Value = Range("B1").Value & " - "
     Else
      ActiveCell.Value = ActiveCell.Value & Chr(10) & Range("B1").Value & " - "
     End If
    End If
    End Sub

    p45cal

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!