Worksheet_Change(ByVal... Not Triggering

jvanbonn

Board Regular
Joined
Mar 11, 2011
Messages
71
Any suggestions why the following code is not triggered when I change the value of cell A1 to TRUE or FALSE? The code is in the active worksheet. All suggestions to make the code more elegant are welcome too.

Rich (BB code):
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address <> "$A$1" Then
  Exit Sub
End If

Application.enableevents = False

Dim h As Long, i As Long, j As Long, k As Long
Dim Rng As Range

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  h is the row index of the selected range (looping)
'  i is the row index of first non-header row of selected range
'  j is the row index of the last row of the selected range
'  k is the kth member of the Range Rng
'
'  Rng is the union of rows in the selected range where
'       column A has the value "TRUE" <= to be hidden
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

If Target.Address = "$A$1" And Target.Value = True Then

  With ActiveSheet.Cells(4, 2).CurrentRegion
  i = 5 '.Item(2, 1).Row                        'FIRST ROW OF DATA
  j = .Item(.Item.Count).Row                 'LAST ROW OF DATA
  End With

  k = 0
    
    For h = i To j Step 1
    
      If ActiveSheet.Cells(h, 1) = True Then
      
        k = k + 1
        
          If k = 1 Then
          
            Set Rng = ActiveSheet.Cells(h, 1)
            Debug.Print Rng.Address
            
          Else
          
            Set Rng = Union(Rng, ActiveSheet.Cells(h, 1))
            Debug.Print Rng.Address
            
          End If
          
      End If
      
    Next h
    
  Rng.EntireRow.Hidden = True

ElseIf Target.Address = "$A$1" And Target.Value = False Then
 
   ActiveSheet.Cells.EntireRow.Hidden = False

End If

Application.enableevents = True

End Sub
Thanks!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
That might not be triggering the event at all.

Yep, just checked.

Clicking a checkbox won't trigger the change event.

Not sure what your code is meant to do but you could try using the checkbox's click event to run it.

You wouldn't be able to use Target but since you know what cell will be changed you don't really need it, so just replace it with Range("A1").

PS The code for finding the last row doesn't seem to work.

If you want to find the last row in a range then take a look at it's Rows (and Row) property.

Something like this perhaps, though I'm not 100% sure it'll work - the headers starting in row 4 seem to be making things a lot harder.:)
Code:
   With ActiveSheet.Cells(4, 2).CurrentRegion
            i = 5    '.Item(2, 1).Row                        'FIRST ROW OF DATA
            j = .Row + .Rows.Count - 1           'LAST ROW OF DATA
        End With
 
Upvote 0
Thanks again!

Very strange, because it used to work. If I manually type TRUE or FALSE in A1, the checkbox changes to checked or unchecked, but the Worksheet_Change(ByVal... still does not trigger.

I'll try your suggestion to trigger it with the checkbox's click event.

PS the last time the code worked I used:
Rich (BB code):
  With ActiveSheet.Cells(4, 2).CurrentRegion
  i = 5 '.Item(2, 1).Row                        'FIRST ROW OF DATA
  j = .Item(.Rows.Count, .Columns.Count).Row   'LAST ROW OF DATA
  'j = .Item(.Item.Count).Row                    'LAST ROW OF DATA
  End With
to determine the last row. I was trying something new, but never could get the code to trigger again.


Another thought I had...

Could the Application.enableevents = False be permanently set? Also, if I try to type ".EnableEvents" with upper case Es, the VB editor changes it to lowercase, which I find really weird.
 
Upvote 0
Well I think the checkbox thing can sometimes be affected by what version of Excel you are using.

It's probably 'safest' to use the click event if you want the code to run when that changes from checked to unchecked or vice versa.

When I tried the original code the part for finding the range failed on .Items.

As for the enable events thing, it is possible events have been accidentally disabled.

You can easily enable them by entering Applicatiion.EnableEvents = TRUE in the Immediate window (CTRL+G).

Perhaps more intriguing is VBE changing EnableEvents to lowercase.

The usual reason for that is because at some point 'EnableEvents' has been used for the name of a variable, a sub, a function etc.

Could that have happened?
 
Upvote 0
I didn't know about typing "Application.EnableEvents = TRUE" in the Immediate window. I learned something new.

And yes, one of my bright ideas for the day was to create a subroutine to set EnableEvents = TRUE. Then I immediately forgot that I did that.:biggrin:

Rich (BB code):
Sub enableevents()
Application.enableevents = True
'MsgBox Application.enableevents
End Sub
However, now that I have deleted that subroutine, I cannot eliminate the lower case "e"s.

It's a good thing I have a sense of humor and can laugh at myself.

Thanks for all you help and insights!
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

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