VBA Code? Move existing task to another row

B

Bruce McNaughton

Hi,

How do I change the position (row) of a task within the Tasks collection
through VBA? I believe that select, cut and paste could work - however, I
do not necessarily have a current view selected - only an active project
object.

The 'index' in the tasks collection appears to be the UniqueID for the task
(Correct?). Is there a field that provides the 'Row' or 'sequence' in the
'entry table' (ie ID? or something else?).

(BTW: I'm aligning MPP Tasks to an external list of tasks where the user
may have changed the order - I keep separate key fields to know where the
relative positions are.)

Any help or pointers appreciated.
 
J

Jan De Messemaeker

Hi Bruce,

This looks like a lot of different topics to me.

First, when you want to import data from a list where the use may have
changed the order, indeed use UniqueID. But mind you the (Index) in the task
collection is the name or the ID; to address a task through its uniqueID use
Tasks.uniqueid(xxxx)

Attention! cutting and pasting changes UniqueID since the pasted task is
considered a new task!!

If you do not have any filter active, and no summary tasks"closed" ID gives
the row number.

But a general advice is to stay away from positions whenever possible. Only
instructions such as FONT that work on the activeselection need the
position; 99.9% of VBA work can and should be done irrespective of position
and even irrespective of the active view.

Hope this helps,
 
B

Bruce McNaughton

Many thanks for the information. The ID = Index and UniqueID = unique across
the tasks within a project. The ID = Row if no filters etc.

Is it possible to change the ID (row) using the SetTaskfield or
SetResourceField methods? Or is there another way to move a task relative to
other tasks and keep all other data in tact?

Thanks again for the help!!

Regards,
Bruce
 
B

Bruce McNaughton

A quick clarification...

I know that tasks can be moved in the 'entry table' using a drag / drop
style operation. How can I emulate the same drag / drop style operation from
VBA?

I tried to record a macro and the drag / drop does not appear to be
available from within the macro recorder.

Any help or pointers appreciated (I appreciate the help so far..)
 
R

Rod Gill

You can't edit the ID number. One way is to copy the ID number to say
Number1
You can then edit the numbers as you want then do a sort with renumber on
the Number1 column. All ID's would then have the same number as the Number1
column.

Try this manually first to get the idea. In fact once you have the sequence
of instructions right you can record a macro to do most of the code for you.
 
J

Jan De Messemaeker

Hi Bruce,

I don't know of a method to emulate the drag and drop.
Cutting and pasting works but as I said it changes uniqueID.
AFAIK nobody ever asked this question and I still fail to see the
importance.
OBTW, I never use SetTaskfield either; pick up the task object then
ThisTask.Text23="any"
Thistask.constraintdate="23/12/05"
etc.
But the ID field cannot be set, it is read only.
HTH
 
J

JackD

Using another field to temporarily hold the value of the id, then modifying
the field for the task you want to move (and the related tasks which need
their ID changed as well) and then sorting by that field and renumbering the
tasks would change the ID. I don't see much reason for it. You could do
something similar by using a value in your external file (call it order or
something) and then import that into a text field and use it to sort.
 
J

Jan De Messemaeker

..... But unless you agree to sort with the option "Keep Outline Structure"
the option "Renumber" is not active.
That is why I did not mention the possibility.
Greetings,
 
B

Bruce McNaughton

Sometimes the 'obvious' is not so 'obvious'!! Using a separate field to
store the expected row order and the Sort function works fine.

Many thanks to everyone that provided pointers and solutions!
 
B

Bruce McF

One use case for "changing task order" ...
We are using a simplified schedule maintenance scheme with (relatively)
straightforward software development projects. Resource Leveling is applied
based on ID Only. The intent is to make simple scheduling more intuitive for
casual users by changing order to reschedule tasks (and avoiding confusing
artificial links and constraints where possible).

Without any grouping, the drag-and-drop allows the casual user to drag a
task to a new location and relevel ... with a predictable, desired result.

With grouping on, however, you can't drag to move (*). We are looking to
VB to allow an intuitive task move operation that works under various
groupings ... so this thread is very relevant to us.

BTW ... If two tasks are selected in a particular order, the link operation
seems to know the sequence and it creates the link in the right direction.
Is the sequence of selection exposed some place for tasks, activeselection,
....???

Thanks,
Bruce McF

(*) Drag many seem problematic under grouping because the result is not
putting the task "between" two known positions. If it simply always put the
task below the destination task, that would be a useful drag-to-move behavior
under grouping.
 
B

Bruce McF

This all works quite well, except that MSP won't Sort/Renumber tasks while
grouped. (It works very well with no task grouping, but we needed to move
within groups. Since my goal is to update ID to support ID Only leveling, I
will add code to detect current grouping, clear it, process, and reset
grouping and see how that works. Thanks .. and always grateful for
additional suggestions.

Thanks,
Bruce
 
W

www.projectserver.cz

On MS project conference onI asked same MS Project executives about
this topic and they told me that there isn't a way haw to change row
order using VBA or by PSI,
 
B

Bruce McF

Saving the current group, applying No Group, processing with a sort/renumber,
and reapplying the original group ... works fine.

The last snag is that I need to reset the selection or otherwise scroll down
to the row where the tasks were moved to. I know the destination task ID,
but I can't determine which row that tasks falls on in the grouped view.
(Moving within the grouped view was the goal.)

Is there a way for me to determine the row number for a particular task? ...
or otherwise scroll the project down to a particular task ... in a grouped
view?

So close....

(... and, Yes, I know this seems like a lot of work, but I am following
through on a particular approach and don't want to give up on it too easily.)
Thanks.
 
B

Bruce McF

Jan,

Thanks! This concept worked well.

Recall that this is to move a task while Grouped. Since I want to return to
the same task (after it is moved) and have it appear at its moved position on
the same screen, I ...

Capture the UniqueID of the task I am moving
Application.ScreenUpdating False
Capture existing Group .. and clear the group
Do the move by sequencing the moved tasks in a customer number field & sorting
Restore the Group

.... then stepping through the tasks until I find the task with the captured
UniqueID
.... and then EditGoTo tsk.ID

Application.ScreenUpdating True
.... and the moved task simply jumps below its destination task as desired (*)

(*) as long as the sort order stays consistent. If there was a sort
difference, then the screen jumps but still brings the moved task into view.
Thereafter, the move operation appears to just cause the task to move to the
right place on an otherwise static screen.

Thanks!
Bruce McF
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top