PRJ: VB Code to Find Blank Rows in Task or Resource Sheet

ID: Q123025

The information in this article applies to:

SUMMARY

When you create a Visual Basic, Applications Edition, procedure to manipulate objects in Microsoft Project, you must test for blank rows in task and resource sheets in order to avoid receiving macro errors.

The following examples contain sample code you can use to test for blank rows.

MORE INFORMATION

Some objects and collections include blank tasks and resources. The following sections detail the differences between the Cell, Project, and Selection objects, and indicate how to test for blank rows.

Microsoft provides examples of Visual Basic procedures for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose.

Cell Object

A Cell object can refer to a cell in a blank row in a task or resource table. If you attempt to get or set properties, or to apply a method with a Task or Resource object associated with such a cell, you will receive a macro error.

To check for this condition, use a macro similar to the following to test whether the object refers to Nothing before you manipulate the object.

 Sub Test()
 Dim oCell As Cell

    Set oCell = ActiveCell
    If oCell.Task Is Nothing Then   ' This is a blank task row
       ...
    Else                            ' This task exists
       ...
    End If

 End Sub

Project Object

Task and Resource collections of a Project object always include blank rows. If you attempt to get or set properties, or if you attempt to apply a method with a Task or Resource object associated with one of these blank rows, you will receive a macro error.

When you loop through these collections, you should always test for blank items. To check for this condition, test whether the object refers to Nothing before you manipulate the object.

 Sub Test()
 Dim oResource As Object

    For Each oResource in ActiveProject.Resources
       If oResource Is Nothing Then   ' This is a blank resource
          ...
       Else                           ' This resource exists
          ...
       End If
    Next oResource

 End Sub

Selection Object

Task and Resource collections of a Selection object behave differently depending on the contents of the selection. If the selection contains only blank rows, then its Task and Resource collections evaluate as Nothing. However, if the selection contains any non-blank rows, then it contains only non-blank rows, so there is no need to actually test for blank rows.

 Sub Test()
 Dim oSel As Selection
 Dim oTask As Object

    Set oSel = ActiveSelection
    If oSel.Tasks Is Nothing Then   ' Selection contains only blank rows
       ...
    Else                            ' Selection contains no blank rows
       For Each oTask In oSel.Tasks
          ...   ' No need to test for blank rows within this loop
       Next oTask
    End If

 End Sub

Additional query words: 4.00
Keywords          : kbcode kbprg
Version           : 4.0 4.1 4.1a 98
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowto

Last Reviewed: November 25, 1997