The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to: Display a String in a Worksheet Cell


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.

This example demonstrates how to display text in a cell programmatically. You can create a named range to reference the cell.

The range object can be one of two types:

Using Host Controls

The following code assumes you have added a NamedRange control named message to a worksheet named Sheet1.

To display text in a NamedRange control

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

    Me.message.Value2 = "Hello world"

Using Excel Objects

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

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"

Compiling the Code

  • The host control code must be placed in a sheet class, not in the ThisWorkbook class.

See Also

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft