Forum Discussion

Jamesp435's avatar
Jamesp435
Brass Contributor
Feb 02, 2022

Project VBA/macros identifying the selected row...

Hi team

I want to use VBA to automate splitting a task on a selected row.  However using 'ActiveProject.Tasks(2).Split' requires a specific row number.  Is it possible to set a variable to a selected row number, as I can then use it to replace the Tasks row number with the variable... or something like that 🙂 

Many thanks 

James 

 

  • James,
    In Project there are two types of VBA macros, those using foreground processing (what yo get when you record a macro) and those that use background processing. Foreground processing operates on the current view (i.e. selecting tasks as they appear in the view). Background processing operates directly on Project objects in Project's database (i.e. the background), independent of what may be displayed in the current view. The most efficient macros operate using background processing but occasionally selection of an item in the current view is the only option.

    When operating on a task object the index is the task ID, not the row number. In a new plan the task ID will also be the row number, but that can change. For example, lets say a filter is applied that displays only tasks 5 through 10. With that selection, task 6 will be on row 2. If you execute the following:
    ActiveProject.Tasks(2).Duration=480
    and expect the task on row 2 to take on a duration of 1 day, you will be mistaken. The task on row 2 will be unchanged but in Project's underlying database, task ID 2 will take on a duration of 1 day (480 minutes).

    It is certainly possible to operate on task elements in a specific display row. The Application.SlectTaskField Method will do exactly that as task row is the first argument. You could also use the ActiveSelection context. For example, if a specific task is selected in the active view, this statement will change the duration of that task to 5 days (4800 minutes assuming a standard 8 hour day):
    ActiveSelection.Tasks(1).Duration=4800
    where the index "1" is the first (and only) task in the selection.

    You started this discussion with a question on how to add a split to selected task. I'm not sure why you would want to do that since the ribbon already has a task split icon in the Task > Schedule group.

    John
  • John-project's avatar
    John-project
    Silver Contributor
    Jamesp435,
    No, it does not require a row number. You've already identified the task (e.g. Tasks(2)) by the task ID. All you need to do is to include the arguments for the Split Method. For example:
    ActiveProject.Tasks(2).Split StartSplitOn:="1/18/22",endspliton:="1/24/22"

    Hope this helps.
    John
    • Jamesp435's avatar
      Jamesp435
      Brass Contributor

      Thanks John but what happens if I want to specify the selected row and not a specific row?  If I selected a cell in Row 3, isn't ...Tasks(2)... relating to the row number 2? 
      Many thanks
      James

      • John-project's avatar
        John-project
        Silver Contributor
        James,
        In Project there are two types of VBA macros, those using foreground processing (what yo get when you record a macro) and those that use background processing. Foreground processing operates on the current view (i.e. selecting tasks as they appear in the view). Background processing operates directly on Project objects in Project's database (i.e. the background), independent of what may be displayed in the current view. The most efficient macros operate using background processing but occasionally selection of an item in the current view is the only option.

        When operating on a task object the index is the task ID, not the row number. In a new plan the task ID will also be the row number, but that can change. For example, lets say a filter is applied that displays only tasks 5 through 10. With that selection, task 6 will be on row 2. If you execute the following:
        ActiveProject.Tasks(2).Duration=480
        and expect the task on row 2 to take on a duration of 1 day, you will be mistaken. The task on row 2 will be unchanged but in Project's underlying database, task ID 2 will take on a duration of 1 day (480 minutes).

        It is certainly possible to operate on task elements in a specific display row. The Application.SlectTaskField Method will do exactly that as task row is the first argument. You could also use the ActiveSelection context. For example, if a specific task is selected in the active view, this statement will change the duration of that task to 5 days (4800 minutes assuming a standard 8 hour day):
        ActiveSelection.Tasks(1).Duration=4800
        where the index "1" is the first (and only) task in the selection.

        You started this discussion with a question on how to add a split to selected task. I'm not sure why you would want to do that since the ribbon already has a task split icon in the Task > Schedule group.

        John

Resources