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

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,057
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,835
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,835
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,922
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,647
Messages
5,488,085
Members
407,624
Latest member
NatashaGillWH

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top