Simple code error..... I think

picklefactory

Well-known Member
Joined
Jan 28, 2005
Messages
508
Office Version
  1. 365
Platform
  1. Windows
Hello folks, hoping for some help please?
A bit embarrassing, but then I am a newbie to VBA
I've made a very simple line of code just to empty one cell based on the contents of another....... YES, just one line of code, and I'm still struggling :(

It works, but I think it is looping or doing something else strange as it crashes Excel when it runs and I have to shut it down, I guess I'm missing something really basic here, but I dunno what I'm afraid.

Help???
Thanks

Code as follows

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("F16") = 1 Then Range("J16").ClearContents
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
The problem is it's an endless loop...

YOu change F16 - This triggers the code.
If F16 = 1, then it clears contents of J16
That changed the value of J16, therefor triggering the code again.
If F16 = 1, then it clears contents of J16
That changed the value of J16, therefor triggering the code again.
If F16 = 1, then it clears contents of J16
That changed the value of J16, therefor triggering the code again.
If F16 = 1, then it clears contents of J16
That changed the value of J16, therefor triggering the code again.

And so on...


Disable events to prevent this, then re-enable them at the end...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Range("F16") = 1 Then Range("J16").ClearContents
Application.EnableEvents = True
End Sub

Hope that helps...
 
Last edited:
Upvote 0
jonmo1
Superb... thank you for such a rapid reply. Told you I'd missed something basic. I hate being a newbie
Thanks again, all OK now of course
 
Upvote 0
What is happening is that every time you clearcontents on J16, the code is being called, since it is effectively a "worksheet change".

To avoid this, we can use a few options:

  1. Turn off/on events:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Range("F16") = 1 Then Range("J16").ClearContents
    Application.EnableEvents = True
    End Sub
  2. Have the code run only when F16 is changed:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("F16")) Is Nothing Then
        If Range("F16") = 1 Then Range("J16").ClearContents
    End If
    End Sub
  3. Use a combination of 1 & 2:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Not Intersect(Target, Range("F16")) Is Nothing Then
        If Range("F16") = 1 Then Range("J16").ClearContents
    End If
    Application.EnableEvents = True
    End Sub
 
Upvote 0
MrKowz

Thanks to you also, you just don't type as quickly as jonmo1 ;)

Much appreciated.
 
Upvote 0
jonmo1
Superb... thank you for such a rapid reply. Told you I'd missed something basic. I hate being a newbie
Thanks again, all OK now of course

Glad to help, thanks for the feedback...


If you're using Event Code, you're not a Newbie anymore...:cool:
 
Upvote 0
Depends on your definition of success...

The code DID in fact successfully do what you wanted....
It was just doing it over and over again....:laugh:

True :biggrin:

I'm better than I thought

Thanks folks, even as simple a thing as that is a worthwhile lesson for numpties of my level, I've learned a number of way of dealing with a few scenarios there.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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