VBA Clearing Range

jc0r

Board Regular
Joined
Mar 16, 2009
Messages
124
Hi all,

I am trying to run a simple bit of script but keep getting an error and Excel becomes non responsive. I have a 3rd party program inputting data into a sheet every 1 second, and when cell "A2" = "Next" i would like the contents of a certain range to be cleared. I am using Excel 2007.

Many thanks in advance

Code:
Private Sub Worksheet_Calculate()

If Sheet3.Cells(2, 1) = "Next" Then
    Range("L4:R6").Select
    Selection.ClearContents
    Range("L8:R500").Select
    Selection.ClearContents
End If
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
It's probably a circular reference type of issue...

Cells calculate - the calculate event triggers
The Calculate event clears contents of cells
the clearing of cells triggers a calculation
The calculation event fires again
Cells are cleared again..Triggering the the calculation event...
Over and over and over again.

You need to disable events, then clear the contents.
Also, you can do it without selecting the cells...

Try

Code:
Private Sub Worksheet_Calculate()
If Sheet3.Cells(2, 1) = "Next" Then
    Application.EnableEvents = False
    Range("L4:R6").ClearContents
    Range("L8:R500").ClearContents
    Application.EnableEvents = True
End If

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,593
Messages
6,179,791
Members
452,942
Latest member
VijayNewtoExcel

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