Cancel=True not working on one worksheet in a workbook

kgartin

Board Regular
Joined
May 6, 2010
Messages
192
I have a workbook with multiple worksheets (Jan - Feb), each with a load of data. Each sheet is set up to change colors (among other things) if a cell is double-clicked and each private sub ends with cancel=true. On one of the sheets (Feb), it just doesn't work (although it HAS worked on that sheet until recently...no relevant changes have been made). All the other sheets function properly and do not go into the cells edit mode when double-clicked. February goes into edit mode every time. The code is the same for each sheet.

Any ideas why this is happening?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

kgartin

Board Regular
Joined
May 6, 2010
Messages
192
Just discovered if I copy the sheet into a new workbook, it works as it should. This one has me scratching my head.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
Can you post the code?
 

kgartin

Board Regular
Joined
May 6, 2010
Messages
192

ADVERTISEMENT

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
     
    On Error GoTo EH 'On Error Resume Next
    'Dim TRGVAL As Integer, TRGFORM As Integer
    With Target.Interior
        If Not .ColorIndex = xlNone Then
            .ColorIndex = xlNone
        ElseIf Not Intersect(Target, Range("F7:P402,V1:V5,AJ7:AJ402")) Is Nothing Then
            If Range("AO1") = True Then
                If Cells(Target.Row, 1) = 1 Then
                    If Cells(Target.Row, 39) = "" And Not Target.HasFormula Then
                        Cells(Target.Row, 39) = Target.Value
                        Cells(Target.Row, 40) = Target.Value
                    End If
                    If Target.HasFormula Then
                        Cells(Target.Row, 39) = Cells(Target.Row, 39) - Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J222").Value
                        If Cells(Target.Row, 39).Value < 0 Then
                            Cells(Target.Row, 39) = ""
                            Cells(Target.Row, 40) = ""
                        End If
                        Target.Value = Target.Formula & "+" & Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J222").Value
                    Else
                        Target.Value = "=AM" & Target.Row & "+" & Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J222").Value
                        Cells(Target.Row, 39) = Cells(Target.Row, 39) - Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J222").Value
                        'If Cells(Target.Row, 39) - Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J222").Value < 0 Then
                            'Cells(Target.Row, 39) = ""
                            'Cells(Target.Row, 40) = ""
                        'End If
                    End If
                Else
                    Target.Value = Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J222").Value
                End If
                If Not Target.Cells.CountLarge > 1 Then
                    If Not Intersect(Target, Range("F7:F402")) Is Nothing Then
                        If Cells(Target.Row, 1) = 1 Then
                            Target.Offset(, 22) = Application.Average(Target.Offset(, 22), Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J227").Value)
                            Else
                            Target.Offset(, 22) = Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J227").Value
                        End If
                        If Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J221") > 0 Then
                            Target.Offset(, 1) = Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J221").Value
                        End If
                    ElseIf Not Intersect(Target, Range("H7:H402")) Is Nothing Then
                        If Cells(Target.Row, 1) = 1 Then
                            Target.Offset(, 21) = Application.Average(Target.Offset(, 21), Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J227").Value)
                            Else
                            Target.Offset(, 21) = Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J227").Value
                        End If
                        If Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J221") > 0 Then
                            Target.Offset(, 1) = Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J221").Value
                        End If
                    ElseIf Not Intersect(Target, Range("J7:J402")) Is Nothing Then
                        If Cells(Target.Row, 1) = 1 Then
                            Target.Offset(, 20) = Application.Average(Target.Offset(, 20), Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J227").Value)
                            Else
                            Target.Offset(, 20) = Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J227").Value
                        End If
                        If Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J221") > 0 Then
                            Target.Offset(, 1) = Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J221").Value
                        End If
                    ElseIf Not Intersect(Target, Range("L7:L402")) Is Nothing Then
                        If Cells(Target.Row, 1) = 1 Then
                            Target.Offset(, 19) = Application.Average(Target.Offset(, 19), Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J227").Value)
                            Else
                            Target.Offset(, 19) = Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J227").Value
                        End If
                        If Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J221") > 0 Then
                            Target.Offset(, 1) = Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J221").Value
                        End If
                    ElseIf Not Intersect(Target, Range("N7:N402")) Is Nothing Then
                        If Cells(Target.Row, 1) = 1 Then
                            Target.Offset(, 18) = Application.Average(Target.Offset(, 18), Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J227").Value)
                            Else
                            Target.Offset(, 18) = Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J227").Value
                        End If
                        If Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J221") > 0 Then
                            Target.Offset(, 1) = Workbooks(Range("AI1").Value).Worksheets("ITEMIZATION").Range("J221").Value
                        End If
                    End If
                End If
            Else
EH:
If Cells(Target.Row, 40) = "" Then Cells(Target.Row, 39).ClearContents
                .ColorIndex = Range("T2").Value
                If Cells(Target.Row, 1) = 1 Then
                    If Left(Target.Formula, 3) = "=AM" Then
                        Dim Refkill As String
                        Refkill = Left(Target.Formula, InStr(1, Target.Formula, "+"))
                        Target = "=" & Replace(Target.Formula, Refkill, "")
                    End If
                End If
            End If
        ElseIf Not Intersect(Target, Range("AJ7:AJ402")) Is Nothing Then
                .ColorIndex = 20
        End If
    End With
'EH:
    Cancel = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Error
    Application.EnableEvents = False


If Range("AN1") <> False Then


    If Not Target.Cells.CountLarge > 1 Then
        If Not Intersect(Target, Range("F7:F402")) Is Nothing Then
            Target.Offset(, 22).Select
        ElseIf Not Intersect(Target, Range("H7:H402")) Is Nothing Then
            Target.Offset(, 21).Select
        ElseIf Not Intersect(Target, Range("J7:J402")) Is Nothing Then
            Target.Offset(, 20).Select
        ElseIf Not Intersect(Target, Range("L7:L402")) Is Nothing Then
            Target.Offset(, 19).Select
        ElseIf Not Intersect(Target, Range("N7:N402")) Is Nothing Then
            Target.Offset(, 18).Select
        End If
    End If


    If Not Target.Cells.CountLarge > 1 Then
        If Not Intersect(Target, Range("AB7:AB402")) Is Nothing Then
            If Not Target.Offset(, -22).HasFormula Then
            Target.Offset(, -22).Interior.ColorIndex = Range("T2").Value
            End If
        ElseIf Not Intersect(Target, Range("AC7:AC402")) Is Nothing Then
            If Not Target.Offset(, -21).HasFormula Then
            Target.Offset(, -21).Interior.ColorIndex = Range("T2").Value
            End If
        ElseIf Not Intersect(Target, Range("AD7:AD402")) Is Nothing Then
            If Not Target.Offset(, -20).HasFormula Then
            Target.Offset(, -20).Interior.ColorIndex = Range("T2").Value
            End If
        ElseIf Not Intersect(Target, Range("AE7:AE402")) Is Nothing Then
            If Not Target.Offset(, -19).HasFormula Then
            Target.Offset(, -19).Interior.ColorIndex = Range("T2").Value
            End If
        ElseIf Not Intersect(Target, Range("AF7:AF402")) Is Nothing Then
            If Not Target.Offset(, -18).HasFormula Then
            Target.Offset(, -18).Interior.ColorIndex = Range("T2").Value
            End If
        End If
    End If
    
    'If Not Range("AJ7:AJ402").ColorIndex = xlNone Then
            '.ColorIndex = xlNone
    If Not Intersect(Target, Range("AJ7:AJ402")) Is Nothing Then
        If Target = "" Then
            Target.Interior.ColorIndex = xlNone
        ElseIf Target <> "" Then
            Target.Interior.ColorIndex = 20
        End If
    End If
    
End If
KeepGoing:
    Application.EnableEvents = True
    Exit Sub
Error:
    MsgBox Err.Description
    Resume KeepGoing


End Sub
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
Have you tried putting a breakpoint on the Cancel = True line to see if it's actually getting executed and/or stepping through the code?
 

kgartin

Board Regular
Joined
May 6, 2010
Messages
192

ADVERTISEMENT

No. I'll try that but like I said, it almost has to be something corrupted. It works on every other sheet and they're identical and it had been working on this one. I've been getting Out of Memory errors and other weird things happening that I've never seen before. Maybe it's the Russians...
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
It could be corruption but there's a lot going on in that code, including the code in the BeforeDoubleClick event triggering the Change event, so debugging like I suggested might shine some light on things.

You might want to consider temporarily 'disabling' the error handling by commenting out the On Error... stuff.
 

kgartin

Board Regular
Joined
May 6, 2010
Messages
192
Wow...I had created the following function that was whacking the sheet out:

Code:
Function IsFormula(c As Range) As Boolean
IsFormula = c.HasFormula
End Function

Why would it cause problems like this?
 
Last edited:

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
Not sure, how was it affecting things?

Was that function being triggered by other code?
 

Forum statistics

Threads
1,141,311
Messages
5,705,680
Members
421,405
Latest member
buster015

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
Top