Clumsy VBA code slows down Excel

BenRichards

New Member
Joined
Apr 7, 2014
Messages
41
Hi,

I've had some 25 of these kinds of IF-statements, and the worksheet has worked quickly:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
.
.
.

If Not Intersect(Target, Range("T82:AA457")) Is Nothing And ActiveSheet.Range("V80").Value = "-" Then
Application.EnableEvents = False
ActiveSheet.Range("T82:AA457").Value = ""
Application.EnableEvents = True
End If

However, I now added 25 other IFs, like this:
Code:
If Intersect(Target, Range("V80")) Is Nothing And ActiveSheet.Range("V80").Value = "-" Then
Application.EnableEvents = False
ActiveSheet.Range("T82:AA457").Value = ""
Application.EnableEvents = True
End If
As a result, whenever a cell is changed (any cell, not just the ones determined as the target ranges), Excel "thinks" for some five seconds and the mouse cursor indicates processing before the new cell value is displayed. Can someone help me?
 
Last edited by a moderator:
And this gives the error type mismatch:

If Not ActiveSheet.Range("AC82:AJ457").Value = "" And ActiveSheet.Range("V80").Value = "-" Then
Application.EnableEvents = False
ActiveSheet.Range("AC82:AJ457").Value = ""
Application.EnableEvents = True
End If
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The Worksheet_Calculate event procedure doesn't have a Target argument, hence the error in your first piece of code. Your second piece of code fails because you can't test the Value of more than one cell for equality. Why does it matter that AC82:AJ457 is empty or not?
 
Upvote 0
In short: if I use Private Sub Worksheet_Calculate(), how can I pinpoint the code to fire only if a certain cell was changed?
 
Upvote 0
Andrew Poulsom, if I leave them like this, all 25 of them:

If ActiveSheet.Range("AE80").Value = "-" Then
Application.EnableEvents = False
ActiveSheet.Range("AC82:AJ457").Value = ""
Application.EnableEvents = True

Then I'm stuck with the some five seconds Excel thinks after each cell-change.
 
Upvote 0
if I use Private Sub Worksheet_Calculate(), how can I pinpoint the code to fire only if a certain cell was changed?

You can't. What are V80's dependents? If they are changed manually you can use those in Worksheet_Change.
 
Upvote 0
You can't. What are V80's dependents? If they are changed manually you can use those in Worksheet_Change.

Sadly they are the result of a formula. V80 can be any letter or number or "-". But this is part of a bigger worksheet, so I can't alter how they are changed.

And if I use the straight IFs that fire at every turn, I'm stuck with the 5-second delay that makes the worksheet user-unfriendly...
 
Upvote 0

Forum statistics

Threads
1,215,873
Messages
6,127,464
Members
449,384
Latest member
purevega

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