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

Last reviewed: November 24, 1997
Article ID: Q123025
The information in this article applies to:
  • Microsoft Project 98 for Windows
  • Microsoft Project for Windows 95, versions 4.1, 4.1a
  • Microsoft Project for Windows, version 4.0
  • Microsoft Project for the Macintosh, version 4.0

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


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 24, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.