Count Property

Microsoft Office Web Components Visual Basic

Returns the number of objects in the specified collection. Read-only Long.

expression.Count

expression    Required. An expression that returns one of the objects in the Applies To list.

Example

This example sets variables to the number of columns and the number of rows in the visible range on the active worksheet, and then formats the color of the cells in every other row.

Sub Format_Odd_Rows()
    Dim rngUsed
    Dim iUsedRows
    Dim iUsedColumns
    Dim shtActive
    Dim iCtr
    
    Set shtActive = Spreadsheet1.ActiveSheet
    
    ' Set a variable ot the used range of the active sheet.
    Set rngUsed = shtActive.UsedRange
    
    ' Get the count of used rows in the active sheet.
    iUsedRows = rngUsed.Rows.Count
    
    ' Get the count of used columns in the active sheet.
    iUsedColumns = rngUsed.Columns.Count
    
    ' Loop through every odd row in the used range.
    For iCtr = 1 To iUsedRows Step 2
    
        ' Color the background of the cells green.
        shtActive.Range(shtActive.Cells(iCtr, 1), shtActive. _
            Cells(iCtr, iUsedColumns)).Interior.ColorIndex = 43
    Next
End Sub