Named Ranges upsetting code

ddub25

Well-known Member
Joined
Jan 11, 2007
Messages
617
Office Version
  1. 2019
Platform
  1. Windows
This is a snippet of code where I have recently adapted some of the cells to Named Ranges and I think it might be upsetting the code as the code isn't working.

Code:
For i = 0 To 9
If Range("SetP1").Value + Range("SetP2").Value = 0 And Range("GaP1").Value + Range("GaP2").Value = i Then
     Range("CT" & (85 + i)).Value = Server
End If
Next i


The code used to be as below and worked fine.

Code:
For i = 0 To 9
If Range("CT73").Value + Range("CT74").Value = 0 And Range("CU73").Value + Range("CU74").Value = i Then
     Range("CT" & (85 + i)).Value = Server
End If
Next i
 
Last edited:
See here for adding breakpoints: Setting Breakpoints and the VBA Stop Statement

There are various different ways to only look at relevant cells, and it often depends on what the range/conditions are. Target is the cell(s) that are updating (if using copy/paste, it could be multiple cells at once).

For example:

To look at column E only
Code:
If Target.Column <> 5 Then
    Exit Sub
End If

To look at a specific cell
Code:
If Target.Address <> "$C$10" Then
    Exit Sub
End If

To see if a cell is found within a specified range (B5:F163)
Code:
If Intersect(Target,Range("B5:F163")) Is Nothing Then
    Exit Sub
End If
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
It seems there wasn't anything wrong with the code adapted to use Named Ranges. I tried rebooting Excel and that solved the problem. The Private Worksheet_Change Sub must have become corrupted even though any normal Sub Routines I have ran fine.

So, Named Ranges that are in fact ranges and not just a reference to a cell are able to be calculated as well.

Joe, thanks for for the additional info on 'Setting Breakpoints and the VBA Stop Statement', and only looking at relevant cells.
 
Upvote 0
I tried rebooting Excel and that solved the problem.
OK, I think I may know what happened here. It is good to know/understand for future reference.

Note at the beginning of your code you have this line:
Code:
Application.EnableEvents = False
and at the end of your code you have this line:
Code:
Application.EnableEvents = True

What this does is temporarily disable events while the code runs, so it doesn't call itself when the code starts changing cell values (leading you into an endless loop!). The line at the end turns it back on.

However, if at some point along the way, you started your code, but then stopped or exited before you got to the last line, then you never turned your events back on, meaning Event Procedure VBA will not run!

You either need to turn it back on by running a simple one line macro like this:
Code:
Sub TurnEventsOn()
    Application.EnableEvents = True
End sub
or if you close Excel and then re-open it, it will reset it back to the default settings (events enabled).
 
Last edited:
Upvote 0
Thanks Joe, very useful to know. Thanks for you help.
 
Upvote 0

Forum statistics

Threads
1,215,652
Messages
6,126,035
Members
449,281
Latest member
redwine77

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