Share via


NamedRange.Change Event (2007 System)

Occurs when something changes in the NamedRange control.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v9.0 (in Microsoft.Office.Tools.Excel.v9.0.dll)

Syntax

'Declaration
Public Event Change As DocEvents_ChangeEventHandler
'Usage
Dim instance As NamedRange 
Dim handler As DocEvents_ChangeEventHandler 

AddHandler instance.Change, handler
public event DocEvents_ChangeEventHandler Change
public:
 event DocEvents_ChangeEventHandler^ Change {
    void add (DocEvents_ChangeEventHandler^ value);
    void remove (DocEvents_ChangeEventHandler^ value);
}
JScript does not support events.

Remarks

This event is raised if the NamedRange control is changed by either a programmatic modification or user interaction. This event does not occur when cells in a NamedRange control change during a recalculation.

Examples

The following code example creates a NamedRange and a Change event handler. To raise the Change event, add text to one of the cells in the NamedRange and then press ENTER.

This version is for a document-level customization.

Private changesRange As Microsoft.Office.Tools.Excel.NamedRange

Private Sub NotifyChanges()
    changesRange = Me.Controls.AddNamedRange( _
        Me.Range("B2", "E5"), "compositeRange")
    AddHandler changesRange.Change, _
        AddressOf changesRange_Change
End Sub 


Sub changesRange_Change(ByVal Target As Excel.Range)
    Dim cellAddress As String = Target.Address(, , _
        Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, , )
    MessageBox.Show("Cell " & cellAddress & " changed.")
End Sub
Microsoft.Office.Tools.Excel.NamedRange changesRange;
private void NotifyChanges()
{
    changesRange = this.Controls.AddNamedRange(
        this.Range["B2", "E5"], "compositeRange");
    changesRange.Change += new Microsoft.Office.Interop.Excel.
        DocEvents_ChangeEventHandler(changesRange_Change);
}

void changesRange_Change(Excel.Range Target)
{
    string cellAddress = Target.get_Address(missing, missing, 
        Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1,
        missing, missing);
    MessageBox.Show("Cell " + cellAddress + " changed.");
}

This version is for an application-level add-in.

Private changesRange As NamedRange

Private Sub NotifyChanges()
    Dim vstoWorksheet As Worksheet = CType( _
        Me.Application.ActiveWorkbook.Worksheets(1),  _
        Excel.Worksheet).GetVstoObject()
    changesRange = vstoWorksheet.Controls.AddNamedRange( _
        vstoWorksheet.Range("B2", "E5"), "compositeRange")
    AddHandler changesRange.Change, _
        AddressOf changesRange_Change
End Sub 


Sub changesRange_Change(ByVal Target As Excel.Range)
    Dim cellAddress As String = Target.Address(, , _
        Excel.XlReferenceStyle.xlA1, , )
    System.Windows.Forms.MessageBox.Show("Cell " & cellAddress & " changed.")
End Sub
NamedRange changesRange;
private void NotifyChanges()
{
    Worksheet vstoWorksheet = ((Excel.Worksheet)
        this.Application.ActiveWorkbook.Worksheets[1]).GetVstoObject();
    changesRange = vstoWorksheet.Controls.AddNamedRange(
        vstoWorksheet.Range["B2", "E5"], "compositeRange");
    changesRange.Change += new Excel.DocEvents_ChangeEventHandler(
        changesRange_Change);
}

void changesRange_Change(Excel.Range Target)
{
    string cellAddress = Target.get_Address(missing, missing,
        Excel.XlReferenceStyle.xlA1,
        missing, missing);
    System.Windows.Forms.MessageBox.Show("Cell " + cellAddress +
        " changed.");
}

.NET Framework Security

See Also

Reference

NamedRange Class

NamedRange Members

Microsoft.Office.Tools.Excel Namespace

Change History

Date

History

Reason

July 2008

Added a version of the code example for an application-level add-in.

SP1 feature change.