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.
 
Without knowing what your other code is, I've no idea.
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Without knowing what your other code is, I've no idea.

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
'Invoice Sent
'First change
If Target.Column = 14 Then
    ActiveSheet.Unprotect Password:="test"
    If Target.Offset(0, 1) = "" Then
        Target.Offset(0, 1) = Date + Time
        Target = "þ"
'All other changes
    Else
        Target.Offset(0, 27) = Date + Time
        If Target = "¨" Then
            Target = "þ"
        Else
            Target = "¨"
        End If
    End If
ActiveSheet.Protect Password:="test"
End If
'Payment Received
'First change
If Target.Column = 16 Then
    ActiveSheet.Unprotect Password:="test"
    If Target.Offset(0, 1) = "" Then
        Target.Offset(0, 1) = Date + Time
        Target = "þ"
'All other changes
    Else
        Target.Offset(0, 26) = Date + Time
        If Target = "¨" Then
            Target = "þ"
        Else
            Target = "¨"
        End If
    End If
    ActiveSheet.Protect Password:="test"
End If
'Entered in GIS
'First change
If Target.Column = 19 Then
    ActiveSheet.Unprotect Password:="test"
    If Target.Offset(0, 26) = "" Then
        Target.Offset(0, 26) = Date + Time
        Target = "þ"
'All other changes
    Else
        Target.Offset(0, 27) = Date + Time
        If Target = "¨" Then
            Target = "þ"
        Else
            Target = "¨"
        End If
    End If
    ActiveSheet.Protect Password:="test"
End If
'Plans Scanned
'First change
If Target.Column = 20 Then
    ActiveSheet.Unprotect Password:="test"
    If Target.Offset(0, 27) = "" Then
        Target.Offset(0, 27) = Date + Time
        Target = "þ"
'All other changes
    Else
        Target.Offset(0, 28) = Date + Time
        If Target = "¨" Then
            Target = "þ"
        Else
            Target = "¨"
        End If
    End If
    ActiveSheet.Protect Password:="test"
End If
End Sub
Do I or should I be using a lot of subs instead of 1? Does that cause it? Sorry all my tabs and spacing is gone in the paste!!! Looks bad.
 
Last edited by a moderator:
Upvote 0
That code is not the problem. You have other event code such as a Worksheet_Change event
 
Upvote 0
That code is not the problem. You have other event code such as a Worksheet_Change event

This. And a few filters

Private Sub Worksheet_Change(ByVal Target As Range)

'Maximo Entered
ActiveSheet.Unprotect Password:="test"
If Target.Column = 18 Then
If Target.Offset(0, 25) = "" Then
Target.Offset(0, 25) = Date + Time
Else
Target.Offset(0, 26) = Date + Time
End If
End If
ActiveSheet.Protect Password:="test"
End Sub
 
Upvote 0
That code is relocking the sheet, which is causing you the problem.
Select this line in the doubleclick event
Code:
   If Target.Column = 12 Then
Then press F9, that line should get a brown highlight.
Then on the sheet doubleclick in col L, the above line of code should be highlighted, press F8 to step through the code & you can watch what happens.
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top