ActiveSelection.Tasks.Count (errors)

F

Finius Eetch

I have a FORM setup that allows the user to select a Start Date, Finish Date,
and Resource Name from a pick list. With that information, I generate a
Filter and custom view.

At this point, my code looks like this

Private Sub CountOfFilteredTasks()
Dim Tasks_Counted
SelectAll
On Error GoTo ErrorHandler
Tasks_Counted = ActiveSelection.Tasks.Count
Exit Sub

ErrorHandler:
MsgBox "No tasks returned by filter"
End Sub


Private Sub Check_Tasks()
Generate_Task_List
FilterApply Name:="TheFilter"
SelectAll
CountOfFilteredTasks

End sub


This works just fine as long as I get some valid tasks back. However, if I
get NO tasks I get Error 424 Object Not Found.

I have a "number of tasks found" box in my form, so all I really need it to
do is show 0, not crash out.

This is nearly the last piece for this particular macro that is holding me
up - so any help would be greatly appreciated.
 
J

John

Finius Eetch said:
I have a FORM setup that allows the user to select a Start Date, Finish Date,
and Resource Name from a pick list. With that information, I generate a
Filter and custom view.

At this point, my code looks like this

Private Sub CountOfFilteredTasks()
Dim Tasks_Counted
SelectAll
On Error GoTo ErrorHandler
Tasks_Counted = ActiveSelection.Tasks.Count
Exit Sub

ErrorHandler:
MsgBox "No tasks returned by filter"
End Sub


Private Sub Check_Tasks()
Generate_Task_List
FilterApply Name:="TheFilter"
SelectAll
CountOfFilteredTasks

End sub


This works just fine as long as I get some valid tasks back. However, if I
get NO tasks I get Error 424 Object Not Found.

I have a "number of tasks found" box in my form, so all I really need it to
do is show 0, not crash out.

This is nearly the last piece for this particular macro that is holding me
up - so any help would be greatly appreciated.

Finius,
First of all I don't know why you have the "Dim" statement. If I recall
correctly not specifying a data type in a Dim statement gives the
variable a variant data type. That is the same as having no Dim
statement at all. However, if you use the Option Explicit statement, you
will need to set up Dim statements for all variable names.

I don't see any issues with your code other than it looks a little more
complex than necessary. Just for reference, I would use the code below.
However, I ran your code and it properly branches to the error handler
message when the filter yields no tasks. Where exactly is the error
occurring when you run it?

My suggested alternate code:
Private Sub Check_Tasks()
Dim Tasks_Counted As Integer
'Generate_Task_List
FilterApply Name:="In Progress Tasks"
SelectAll
On Error Resume Next
Tasks_Counted = ActiveSelection.Tasks.count
If Err > 0 Then
MsgBox "No tasks returned by filter"
On Error GoTo 0 'this resets the error handler
End
End If

End Sub

John
Project MVP
 
R

Rod Gill

Hi,

Try:

Private Sub CountOfFilteredTasks()
Dim Tasks_Counted As Integer
SelectAll
If ActiveCell.Task Is Nothing Then
Tasks_Counted = 0
Else
Tasks_Counted = ActiveSelection.Tasks.Count
End If
End Sub
 

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