Share via


Responding to the Change Event of a NamedRange View Control

Have you ever wanted to write code to respond to data changes in a Microsoft Office Excel range? In Microsoft Visual Studio Tools for the Microsoft Office System, Version 2003, ranges do not have events. What you have to do is write code in the Change event of the Worksheet, and then figure out if the range that was changed is the one you're interested in. I wrote about responding to the Change event of a named range in the Visual Studio Tools for Office, Version 2003 blog.

In Visual Studio Tools for Office, Version 2.0 you can write code against the events of a NamedRange view control (or any other view control) directly. When you add a named range to a worksheet, Visual Studio Tools for Office, Version 2.0 automatically creates a first class object with events you can access directly.

Here's how you'll be able to do this in Version 2.0:

After you have added a NamedRange view control named "test", you simply add your code to the Change event of test:

' Visual Basic
Private Sub test_Change(ByVal Target As _
Microsoft.Office.Interop.Excel.Range)
Handles test.Change

MessageBox.Show("The named range 'test' has changed.")

End Sub

// C#
void test_Change(Microsoft.Office.Interop.Excel.Range Target)
{
MessageBox.Show ("The named range 'test' has changed.");
}

If you have multiple named ranges in your worksheet, you can write an event handler for each named range, or write an event handler to respond to several of the named ranges.

-- Kathleen McGrath

-----
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm.