How to: Send Values to Worksheet Cells


Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

You can send a value to a cell in a worksheet by assigning the value to the Value2 property of the named range. Place the following code in the Sheet1 class:

To send a value to a named range of a worksheet

  • Create a NamedRange control on cell A1 of Sheet1, and assign a string value to the Value2 property.

    Microsoft.Office.Tools.Excel.NamedRange nr =
        this.Controls.AddNamedRange(this.Range["A1", missing], "NamedRange1");
    nr.Value2 = "Hello World";

Compiling the Code

  • This code must be placed in a sheet class, not in the ThisWorkbook class.

Robust Programming

The following condition causes an exception:

  • The worksheet exists and is read-only (COMException class).


User input can include information that is potentially malformed, malicious, or not valid. Always check that the values that a user enters conform to the requirements for your application before using the values. For more information, see Validation of Control Data on Windows Forms

See Also

Community Additions