vba object error

L

Lynn

hi, can someone help me to fix this?

Do While 0 < Count
If Task.Finish > DateTime.Now Then
Font Color:=1
End If
Loop
 
R

Rod Gill

Hi,

Two things, have you tested for a null task (blank row)? Task is a reserved
work so is invalid code. For the active task use Activecell.task



Rod Gill
Microsoft MVP for Project

Author of the only book on Project VBA, see:
http://www.projectvbabook.com
 
L

Lynn

hi,

could you further elaborate what does reserved work means?

if possible post a sample code here. thanks
 
J

Jan De Messemaeker

Hi,

Try this

selectbeginning
for ctr=1 to activeproject.tasks.count
selectrow(1)
set Job=activeselection.tasks(1)
if not job is nothing then
if job.finish>VBA.date then
font color:=1
end if
end if
next ctr

Hope this helps,

--
Jan De Messemaeker
Microsoft Project Most Valuable Professional
+32 495 300 620
For availability check:
http://users.online.be/prom-ade/Calendar.pdf
 
R

Rod Gill

Sorry, reserved word, like do, loop, if, then etc.

--

Rod Gill
Microsoft MVP for Project

Author of the only book on Project VBA, see:
http://www.projectvbabook.com



hi,

could you further elaborate what does reserved work means?

if possible post a sample code here. thanks
 
L

Lynn

same error

Hi,

Eliminate blank lines first. Does it then still happen?
Greetz,

--
Jan De Messemaeker
Microsoft Project Most Valuable Professional
+32 495 300 620

ran into this error

run-time error '424':
Object required.








- Show quoted text -
 
J

Jack Dahlgren MVP

Something like this may work:

Sub colorMe()
OutlineShowAllTasks
Dim t As Task
For Each t In ActiveProject.Tasks
If Not t Is Nothing Then
If t.Finish > Now() Then
Find Field:="ID", Test:="equals", Value:=t.ID, Next:=True, MatchCase:=False
Font Color:=pjRed
End If
End If
Next t
End Sub

-Jack Dahlgren


same error
 
L

Lynn

Excellent!

Could you explain what does the following code means?
Dim t As Task
For Each t In ActiveProject.Tasks
 
D

Dave

Each project has a set of tasks. The ActiveProject.Tasks gives access
to the set of tasks that comprise the active project.

The 'For' construct allows you to iterate through each of the tasks in
the active project. It will execute the block of code between itself
and the closing 'Next t' for each of the tasks it finds within the
active project.

The first line you quote simply reserves storage space for the task that
the routine is currently working on.
 
L

Lynn

i am trying very hard to understand Dim statements.
when do we need to use Dim and when do we use Set?

sorry i know this is very basic, but after reading books on vba i am
still have trouble starting to write my own code....
 
D

Dave

Dim simply reserves storage space but at that point the contents of the
variable reserved have no meaningful content.

Set is used when you want a variable to refer to a declared object but
which you don't want to make a copy of.
 
J

Jack Dahlgren MVP

Dim is used to declare a variable.
In VBA it is not strictly necessary - just using a variable name will create
the variable - but it is a good practice and it makes understanding the code
easier.
This is a very simple bit of code, but if you wanted to work with a task and
subtasks you would want to declare a variable for the parent task and then
perhaps one to iterate among all the subtasks.

Declaring variables is a good practice because you want to make sure that
you are using the same variable throughout. For example if you make a typo
and use tsk in one place and tks in another then you may not get the results
you are looking for. Declaring the variable allows the autosense technology
work with the variable as well, suffixing the methods or properties
properly. If you add the line "Option Explicit" at the top of the module
containing the macro then you will be forced to declare your variables
before using them. This is also a good practice for beginners as it prevents
you from making some simple errors.

Set is used to set a variable equal to whatever you are setting it to.
Frequently I use this:

dim ts as Tasks
set ts = ActiveProject.Tasks

Then work with ts. This makes typing easier and often clearer and less error
prone.
Set and Dim are not particularly related or exclusive.

-Jack Dahlgren

i am trying very hard to understand Dim statements.
when do we need to use Dim and when do we use Set?

sorry i know this is very basic, but after reading books on vba i am
still have trouble starting to write my own code....
 
R

Rod Gill

For Each is standard VBA code, but ActiveProject.Tasks is obviously an
extension to VBA relevant only to Project VBA. When you write code, over its
life you will usually spend far more time maintaining the code that it took
to write it, or you will spend more time editing it after copying it to
another project for a different Macro. So what Jan is saying is that good
practice makes code that is easier to understand which means quicker,
cheaper to maintain and re-use.

--

Rod Gill
Microsoft MVP for Project

Author of the only book on Project VBA, see:
http://www.projectvbabook.com



i am trying very hard to understand Dim statements.
when do we need to use Dim and when do we use Set?

sorry i know this is very basic, but after reading books on vba i am
still have trouble starting to write my own code....
 
L

Lynn

I tried repeating this code to colour tasks which t.Finish < Now() to
pjBlack. But it does not work. I only works for the top code for
pjRed.

any help?
 
J

Jan De Messemaeker

HI,

Between False and Font you have to insert a "New Line"
HTH

--
Jan De Messemaeker
Microsoft Project Most Valuable Professional
+32 495 300 620
For availability check:
http://users.online.be/prom-ade/Calendar.pdf
I tried repeating this code to colour tasks which t.Finish < Now() to
pjBlack. But it does not work. I only works for the top code for
pjRed.

any help?
 

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