ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

VBA: How to Reference Excel Cells

Updated on September 4, 2012

One of the first things you will need to learn about Excel is how to use cells. This module will detail selecting, entering data, and modifying contents of cells in both Excel and Visual Basic Code.

Selecting Cells

To Select a Cell on the Sheet Simply Quick within its Boundaries. When a cell is selected its border will appear in bold.

A selected cell is also known as the active cell.

Understanding a Cell Address

A cell address allows you to refer to a cell by it specific coordinates on your sheet. Specifically the Column letter followed by the row number.

i.e. A15 would be the fiteenth row in the first column.

Bellow is a sample of cells with their addresses in them.

Cell Address Example

Going to a Cell using the Name Box

You can go directly to cell location typing its address in the name toolbox.

Entering Data into a Cell

To enter data into the selected cell. Simply type the desired text and press enter.

Note: Using this method will delete any data previously in the cell.

Modifying the Contents of a Cell

If you have previously entered data in a cell and want to modify it. Make a double left mouse click on the cell. You will then enter a mode that allows you to edit cell contents.

Referencing Cells in Visual Basic

There are two simple methods for referencing a cell in VBA.

The Range Method:

This method allows you to refer to a Cell in VBA by using its address.

i.e. Range("A1")

This is simple and useful in many situations.

The Cells Method:

This method allows you to select a Cell by defining its numeric coordinates (row,column).

i.e. Cells(1,1) would refer to the cell in the first row of the first column, or range A1

This method is very useful because you can easily use a numeric variable for the row or column coordinate.

Referencing Cell using the Cells Method

Selecting a Cell in Visual Basic


'The above Code would select cell "A1" making it the active cell.


'This code will preform the same action.

Entering Data into a Cell from VBA

To enter data into a cell in visual basic simply reference the cell followed by the equal sign

Each couplet of code below perform equivalent functions.



Note: Enclose Text in quotes, do not include numeric values in quotes



A Sample Macro

sub test()
End sub

The code above enters values in cells using both methods and then selects cell "B12", making it the active cell, just for the fun of it. Practice selecting and entering values in cells on your own until you feel comfortable.

Note: Unlike in the Excel user interface a cell doesn't have to be select to change its properties in VBA.

A common pitfall of new VBA users is unnecessarily selecting cells.

If you are completely new to VBA check out my module Navigating the VBA Environment.

The End Result

Ready to Learn More

In my next module we will cover clearing, deleting, and inserting cells. Click here when you are ready to proceed.


    0 of 8192 characters used
    Post Comment

    No comments yet.