populating list boxes using VBA

P

ProjectUser

I’m attempting to create a form using VBA…I’m a novice.
I need to populate a listbox with summary tasks (level 2 tasks only) and I’m
not sure how to do this. Also, I need to populate a listbox with all
resource names.
Can someone help me with code?
Thanks!
 
J

Jack Dahlgren

ProjectUser said:
I'm attempting to create a form using VBA.I'm a novice.
I need to populate a listbox with summary tasks (level 2 tasks only) and I'm
not sure how to do this. Also, I need to populate a listbox with all
resource names.
Can someone help me with code?
Thanks!

These are the basics. Make a user form with your listbox (here named
listbox1). Put this in your initialize event (or on a button click if you
like).

Private Sub UserForm_Initialize()
For Each Task In ActiveProject.Tasks
If Task.Summary And Task.OutlineLevel = 2 Then
ListBox1.AddItem (Task.Name)
End If
Next Task
End Sub

Obviously the usual check for blank tasks needs to happen.

-Jack Dahlgren
 
P

ProjectUser

Thanks!

Jack Dahlgren said:
These are the basics. Make a user form with your listbox (here named
listbox1). Put this in your initialize event (or on a button click if you
like).

Private Sub UserForm_Initialize()
For Each Task In ActiveProject.Tasks
If Task.Summary And Task.OutlineLevel = 2 Then
ListBox1.AddItem (Task.Name)
End If
Next Task
End Sub

Obviously the usual check for blank tasks needs to happen.

-Jack Dahlgren
 
P

ProjectUser

Hi Jack,

I ran the form and get an error message ("Variable not defined") with "task"
being highlighted.

Am I missing something here?

Thanks again,
 
J

Jack Dahlgren

Sounds like you may have blank lines in your project. Please get rid of
them.
You can test for blank lines with an

if not Task is nothing then...

Or maybe you have not defined task and you have option explicit. Define a
variable for the task.
I usually use:

dim t as task
dim ts as tasks
set ts = activeproject.tasks

for each t in ts
'do something
next t

-Jack Dahlgren
 
R

Rod Gill

Hi Jack,

I think the problem is that Task is a reserved name. You probably wrote the
code from memory (fatal at our age!!!)

Try:
Private Sub UserForm_Initialize()
Dim Tsk As Task
For Each Tsk In ActiveProject.Tasks
If Tsk.Summary And Tsk.OutlineLevel = 2 Then
ListBox1.AddItem (Tsk.Name)
End If
Next Task
End Sub


--

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
 
P

ProjectUser

Thanks gentleman! Both suggestions helped.

Although Rod, "Next Task" should be "Next Tsk"

Rod, you may "see" me in your VBA class someday soon!!
 

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