Automatic Resource Assigning

J

JD

Hey guys, I'm importing my resources from another table, and they have a
column with an ID (Number1) that corresponds to which task they belong to.
This is the code I'm working with right now:

Sub Automatic_Resource_Assignment()

Dim Temp1 As Long
Dim Temp2 As Long

For Temp1 = 1 To ActiveProject.Resources.Count

For Temp2 = 1 To ActiveProject.Tasks.Count

If ActiveProject.Resources(Temp1).Number1 =
ActiveProject.Tasks(Temp2).ID Then ActiveProject.Tasks(Temp2).Assignments.Add
(ActiveProject.Resources(Temp1).ID)

Next Temp2

Next Temp1

End Sub

I'm getting an error on the long line saying the argument value is not
valid. My question is what is causing this error, and if this method should
work if I get it to run.
 
J

JD

Thanks, this works perfectly now. Are there any optimizations you can see to
make it run faster?
 
J

Jan De Messemaeker

Hi,

Quoting from memory (but what's the memory of an old man) , it could run
faster in a completely different way: if you have a list of the tasks, and
for each task the IDs of the resoruces. Then you could format the
ResourceNames field and go
Task.resourcenames = The string you compose

But I'm not sure :-|
--
Jan De Messemaeker
Microsoft Project Most Valuable Professional
+32 495 300 620
For availability check:
http://users.online.be/prom-ade/Calendar.pdf
 
J

JD

Ok, this is very strange. It only ran properly the first time. If I try to
run it again it gives me the error again
 
R

Rod Gill

Hi,

You can only have a resource once on a task. Run the code again without
first deleting the assignment will cause an error.

I don't think you need to loop thru all tasks:

Sub Test()
Dim Res As Resource
On Error Resume Next
For Each Res In ActiveProject.Resources
If Res.Number1 > 0 Then
Res.Assignments.Add TaskID:=Res.Number1
End If
Next Res
End Sub

On error resume next makes the code continue if there is an error.


--

Rod Gill
Project MVP

Project VBA Book, for details visit:
http://www.projectvbabook.com

NEW!! Web based VBA training course delivered by me. For details visit:
http://projectservertraining.com/learning/index.aspx
 
J

JD

Adding ' On Error Resume Next ' to the previous way of doing it, does work,
however.
 

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