Using range names with Worksheet_Change

ElvisSteel

Board Regular
Joined
Mar 23, 2009
Messages
122
Hi,

I need to run some code whenever a user changes the value in certain cells.
Currently the code is...
Code:
If Not Intersect(Target, Range("C4")) Is Nothing Then
  Do Something
End If
or sometimes...
Code:
 If ActiveCell.Address = "$C$4" Then
  Do Something
End If
which work fine, but I want to refer to a defined name rather than C4, so that if the address changes due to inserting/deleting rows or columns I don't have to change the code.
Is this possible?
I have tried
Code:
If Not Intersect(Target, Range("RangeName")) Is Nothing Then
  Do Something
End If
Have I missed something obvious?

Thanks
 

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,731
Office Version
365
Platform
Windows
Hi

How is the name defined?

Perhaps;

Code:
If Not Intersect(Target, [COLOR=red]Sheets("Sheet1").[/COLOR]Range("RangeName")) Is Nothing Then
 

ElvisSteel

Board Regular
Joined
Mar 23, 2009
Messages
122
Jon,

Thanks for such a prompt response.

The original error I am getting is:
Method 'Range' of object '_worksheet' failed

After making your suggested change to include the sheet name I now get:
Application-defined or object-defined error

The range name in question is scoped to the workbook.

Thanks
 

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,731
Office Version
365
Platform
Windows
There is bound to be an easier way but all I could think of is:

Code:
[COLOR="Blue"]Private[/COLOR] [COLOR="Blue"]Sub[/COLOR] Worksheet_SelectionChange([COLOR="Blue"]ByVal[/COLOR] Target [COLOR="Blue"]As[/COLOR] Range)
    [COLOR="Blue"]Dim[/COLOR] strRngAddress [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
    strRngAddress = ActiveWorkbook.Names("test").RefersTo
    strRngAddress = [COLOR="Blue"]Mid[/COLOR](strRngAddress, 3, [COLOR="Blue"]Len[/COLOR](strRngAddress) - 3)
    
    [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] Application.Intersect(Target, Range(strRngAddress)) [COLOR="Blue"]Is[/COLOR] [COLOR="Blue"]Nothing[/COLOR] [COLOR="Blue"]Then[/COLOR]
        [COLOR="Blue"]MsgBox[/COLOR] "beep"
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 

Forum statistics

Threads
1,082,046
Messages
5,362,873
Members
400,696
Latest member
Kclynn

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top