Partager via


How to: Programmatically Display a String in a Worksheet Cell

This example demonstrates how to display text in a cell programmatically. To display text in cell, use either a NamedRange control or a native Excel range object.

Applies to: The information in this topic applies to document-level projects and application-level projects for Excel 2013 and Excel 2010. For more information, see Features Available by Office Application and Project Type.

Using a NamedRange Control

This example uses a NamedRange control named message. The control must be added to a document-level customization at design time. The following code must be placed in a sheet class, not in the ThisWorkbook class.

To display text in a NamedRange control

  • Set the value of the NamedRange control to Hello World.

    Me.message.Value2 = "Hello world"
    
    this.message.Value2 = "Hello world";
    

Using a Native Excel Range

The following code creates a new range programmatically and then assigns a value to it.

To display text in an Excel range

  • Retrieve the range at cell A1 on Sheet1 and set the value to Hello World.

    Dim rng As Excel.Range = Globals.Sheet1.Range("A1")
    rng.Value2 = "Hello world"
    
    Excel.Range rng = Globals.Sheet1.Range["A1"];
    rng.Value2 = "Hello world";
    

See Also

Tasks

Walkthrough: Collecting Data Using a Windows Form

Concepts

NamedRange Control

Global Access to Objects in Office Projects

Optional Parameters in Office Solutions

Other Resources

Troubleshooting Office Solutions