Confusing Code

Kevin0427

Board Regular
Joined
Mar 31, 2016
Messages
69
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)



'Plan Rejected
If Target.Column = 12 Then
ActiveSheet.Unprotect Password:="test"
Target.Offset(0, -6) = 0
'Target.Offset(0, 1) = Date + Time
Target = "þ"
'Range("b" & Target.Row & ":bz" & Target.Row).Locked = True
ActiveSheet.Protect Password:="test"
End If

I am confused by the following code. The two rows that you see as commented out do not run when they are uncommented. If I switch the two target.offset codes the first one always runs and the second one does not. The locked range used to work until I added the second offset and now it does not run even if bad code gets commented out.
 

Some videos you may like

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,055
Office Version
2013
Platform
Windows
Kevin, be more specific and use... Target.Value = "þ" and that should hopefully do it.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,630
Office Version
365
Platform
Windows
You're code works perfectly well for me.
Do you have any other Event code working on that sheet?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,630
Office Version
365
Platform
Windows
In that case try
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Plan Rejected
   If Target.Column = 12 Then
      Cancel = True
      Application.EnableEvents = False
      ActiveSheet.Unprotect Password:="test"
      Target.Offset(0, -6) = 0
      Target.Offset(0, 1) = Date + Time
      Target = "þ"
      Range("b" & Target.Row & ":bz" & Target.Row).Locked = True
      ActiveSheet.Protect Password:="test"
   End If
   Application.EnableEvents = True
End Sub
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,920
Office Version
2019
Platform
Windows
try this update

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)


    On Error GoTo exitsub
    Me.Unprotect Password:="test"
    
'Plan Rejected
    With Target
        If .Column = 12 Then
        Application.EnableEvents = False
        Cancel = True
            .Offset(0, -6).Value = 0
            .Offset(0, 1).Value = Now()
            .Value = "þ"
        End If
    End With
    Me.Range("b" & Target.Row).Resize(, 77).Locked = True
    
exitsub:
    Me.Protect Password:="test"
    Application.EnableEvents = True
End Sub
Dave
 
Last edited:

Kevin0427

Board Regular
Joined
Mar 31, 2016
Messages
69
In that case try
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Plan Rejected
   If Target.Column = 12 Then
      Cancel = True
      Application.EnableEvents = False
      ActiveSheet.Unprotect Password:="test"
      Target.Offset(0, -6) = 0
      Target.Offset(0, 1) = Date + Time
      Target = "þ"
      Range("b" & Target.Row & ":bz" & Target.Row).Locked = True
      ActiveSheet.Protect Password:="test"
   End If
   Application.EnableEvents = True
End Sub
I meant to quote this. It worked. Why do I not need it in the rest of the event codes?
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,102,301
Messages
5,486,045
Members
407,529
Latest member
netojose

This Week's Hot Topics

Top