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 



5 Replies
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.

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

best response confirmed by Jamesp435 (Contributor)
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:
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):
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.

Thanks for this John



You're welcome and thanks for the feedback.