Home

Introduction to Cells

 

A Cell in a Worksheet

 

Introduction

A spreadsheet is a series of columns and rows. These columns and rows intersect to create cells:

When Microsoft Excel starts, it creates 16,384 columns and 1,048,576 rows. As a result, a Microsoft Excel spreadsheet has 16,384 * 1,048,576 = 17,179,869,184 cells available.

The Active Cell 

To access a cell, you can click it. This becomes the active cell. In VBA, the active cell is represented by an object named ActiveCell.

Referencing Cells

To identify a cell, you can use the Range object. In the parentheses of the Range object, pass a string that contains the name of the cell. Here is an example that refers to the cell located as D6:

Sub Exercise()
    Workbooks.Item(1).Worksheets.Item("Sheet1").Range("D6")
End Sub

To get a reference to a cell, declare a variable of type Range. To initialize the variable, identify the cell and assign it to the variable using the Set operator. Here is an example:

Sub Exercise()
    Dim Cell As Range
    Set Cell = Workbooks.Item(1).Worksheets.Item("Sheet1").Range("D6")

End Sub

Cells are referred to as adjacent when they touch each other. To refer to a group of adjacent cells, in the parentheses of the Range object, pass a string that is made of the address of the cell that will be on one corner, followed by a colon, followed by the address of the cell that will be on the other corner. Here is an example:

Sub Exercise()
    Range("B2:H6")
End Sub

You can use this same technique to refer to one cell. To do this, use the same cell address on both sides of the colon. Here is an example:

Sub Exercise()
    Range("D4:D4")
End Sub

Instead of referring to one group of adjacent cells, you can refer to more than one group of non-adjacent cells. To do this, pass a string to the Range object. In the string, create each range as you want but separate them with commas. Here is an example:

Sub Exercise()
    Range("D2:B5, F8:I14")
End Sub

Selecting Cells

 

Introduction 

Before doing anything on a cell or a group of cells, you must first select it. To support cell selection, the Range object is equipped with a method named Select. Therefore, to programmatically select a cell, after referencing it, call the Select method. Here is an example:

Sub Exercise()
    Range("D6").Select
End Sub

When you have selected a cell, it is stored in an object named Selection. You can use this object to take an action on the cell that is currently selected.

Selecting Cells

To programmatically select a group of adjacent cells, refer to the group using the techniques we saw earlier, then call the Select method.

To programmatically select all cells of a column, access the Columns collection and pass the column name as a string, then call the Select method. Here is an example we saw in Lesson 9:

Sub Exercise()
    Rem This selects all cells from the fourth column
    Columns(4).Select
End Sub

To perform this operation using the name of a column, pass that name as argument. Here is an example that selects all cells from Column ADH:

Sub Exercise()
    Rem This selects all cells from the column labeled ADH
    Columns("ADH").Select
End Sub

You can also perform this operation using the Range object. To do this, use the Range collection. In the parentheses of the colection, enter the name of the column, followed by a colon, followed by the same column name. Here is an example:

Sub Exercise()
    Rem This selects all cells from Column G
    Range("G:G").Select
End Sub

To programmatically select all cells that belong to a group of adjacent columns, in the parentheses of the Columns collection, enter the name of the first column on one end, followed by a colon ":", followed the name of the column that will be at the other end. Here is an example:

Sub Exercise()
    Rem This selects all cells in the range of columns from Column D to Column G
    Columns("D:G").Select
End Sub

To select the cells that belong to a group of non-adjacent columns, use the technique we saw earlier to refer to non-adjacent columns, then call the Select method. Here is an example:

Sub Exercise()
    Rem This selects the cells from columns B, D, and H
    Range("H:H, D:D, B:B").Select
End Sub

To programmatically select all cells that belong to a row, access a row from the Rows collection, then call the Select method. Here is an example that all cells from Row 6:

Sub Exercise()
    Rows(6).Select
End Sub

You can also use the Range object. After accessing the row, call the Select method. Here is an example that selects all cells from Row 4:

Sub Exercise()
    Range("4:4").Select
End Sub

To select all cells that belong to a range of rows, refer to the range and call the Select method. Here is an example that selects all cells that belong to the rows from 2 to 6:

Sub Exercise()
    Rows("2:6").Select
End Sub

To select all cells that belong to non-adjacent rows, refer to the rows and call the Select method. Here is an example that selects all cells belonging to Rows 3, 5, and 8:

Sub Exercise()
    Range("3:3, 5:5, 8:8").Select
End Sub

To programmatically select cells in the same region, enter their range as a string to the Range object, then call the Select method. Here is an example:

Sub Exercise()
    Range("B2:H6").Select
End Sub

Remember that you can use the same technique to refer to one cell, thus to select a cell. Here is an example:

Sub Exercise()
    Range("D4:D4").Select
End Sub

To select more than one group of non-adjacent cells, refer to the combination as we saw earlier and call the Select method. Here is an example:

Sub Exercise()
    Range("D2:B5, F8:I14").Select
End Sub

To select all cells of a spreadsheet, you can call the Select method on the Rows collection. Here is an example:

Sub Exercise()
    Rows.Select
End Sub

Instead of the Rows collection, you can use the Columns collection instead and you would get the same result.

When you have selected a group of cells, the group is stored in an object named Selection. You can use this object to take a common action on all of the cells that are currently selected.

The Name of a Cell

We already saw that, to refer to a cell using its name, you can pass that name as a string to the Range object.

After creating a name for a group of cells, to refer to those cells using the name, call the Range object and pass the name as a string.

 
 
 

The Gridlines and Headings of a Worksheet

 

Showing the Gridlines of Cells

To show or hide the gridlines, call the ActiveWindow and access its DisplayGridlines property. This is a Boolean property. If you set its value to True, the gridlines appear. If you set it to False, the gridlines disappear. Here is an example of using it:

Sub Exercise()
    ActiveWindow.DisplayGridlines = False
End Sub

Showing the Headings of a Worksheet

To show or hide the headers of columns, get the ActiveWindow object and access its DisplayHeadings Boolean property. To show the headers, set this property to True. To hide the headers, set the property to False. Here is an example:

Sub ShowHeadings()
    ActiveWindow.DisplayHeadings = False
End Sub

Operations on Cells

 

Adding Cells

We know that, to insert a column made of (vertical) cells, you can access the Columns collection, specify an index in its parentheses, and call the Insert method. Here is an example:

Sub CreateColumn()
    Columns(3).Insert
End Sub

We also know how to create a series of rows made of cells horizontally.

Cell Data Entry

Data entry consists of adding one or more values into one or more cells. This can be done manually, automatically, or programmatically. We already know that a cell that is currently selected in a worksheet is called ActiveCell. Therefore, to programmatically add a value to the active cell, assign that value to this object.

Practical LearningPractical Learning: Introducing Data Entry

  1. Start Microsoft Excel
  2. On the Ribbon, click Developer
  3. In the Code section, click Record Macro
  4. Set the Macro Name to CreateWorkbook
  5. In the Shortcut Key text box, type W to get Ctrl + Shift + W
     
    Record Macro
  6. Click OK
  7. On the Ribbon, click Stop Recording
  8. In the Code section of the Ribbon, click Macros Macros
  9. In the Macro dialog box, make sure CreateWorkbook is selected and click Edit
  10. Change the code as follows:
     
    Option Explicit
    
    Sub CreateWorkbook()
        ' CreateWorkbook Macro
        ' This macro is used to create a workbook for the
        ' Georgetown Dry Cleaning Services
    
        ' Keyboard Shortcut: Ctrl+Shift+W
        
        Rem Just in case there is anything on the
        Rem worksheet, delete everything
        Range("A:K").Delete
        Range("1:20").Delete
        
        Rem Create the sections and headings of the worksheet
        Range("B2") = "Georgetown Dry Cleaning Services"
        Range("B5") = "Order Identification"
        Range("B6") = "Receipt #:"
        Range("G6") = "Order Status:"
        Range("B7") = "Customer Name:"
        Range("G7") = "Customer Phone:"
        
        Range("B9") = "Date Left:"
        Range("G9") = "Time Left:"
        Range("B10") = "Date Expected:"
        Range("G10") = "Time Expected:"
        Range("B11") = "Date Picked Up:"
        Range("G11") = "Time Picked Up:"
              
        Range("B13") = "Items to Clean"
        Range("B14") = "Item"
        Range("D14") = "Unit Price"
        Range("E14") = "Qty"
        Range("F14") = "Sub-Total"
    
        Range("B15") = "Shirts"
        Range("H15") = "Order Summary"
        Range("B16") = "Pants"
        Range("B17") = "None"
        Range("H17") = "Cleaning Total:"
        Range("B18") = "None"
        Range("H18") = "Tax Rate:"
        Range("I18") = "5.75"
        Range("J18") = "%"
        Range("B19") = "None"
        Range("H19") = "Tax Amount:"
        Range("B20") = "None"
        Range("H20") = "Order Total:"
        
        Rem Change the widths and heights of some columns and rows
        Rem In previous lessons, we learned all these things
        Range("E:E, G:G").ColumnWidth = 4
        Columns("H").ColumnWidth = 14
        Columns("J").ColumnWidth = 1.75
        
        Rows("3").RowHeight = 2
        Range("8:8, 12:12").RowHeight = 8
        
        Rem Hide the gridlines
        ActiveWindow.DisplayGridlines = False
    End Sub
  11. To return to Microsoft Excel, click the View Microsoft Excel button View Microsoft Excel
  12. To fill the worksheet, press Ctrl + Shift + W
     
    Georgetown Dry Cleaning Services
  13. Close Microsoft Excel
  14. When asked whether you want to save, click No
 
 
   
 

Previous Copyright © 2007-2009 FunctionX, Inc. Next